You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Sebastian Esponda <se...@gmail.com> on 2015/12/16 23:29:41 UTC

Adding custom fields dynamically to the JSON layout output.

Hi,

When using the JSON layout, we'd like to know if it's possible to log
objects (instead simple Strings) so that the JSON output includes the
object itself (serialised by Jackson, which will discover the properties to
be serialised, adding the fields to the JSON structure accordingly)

In other words, instead of:

{...,"message": "some string", ... }

Something similar this:

log.debug(MyObj("a","b"))
{...,"MyObj": { "prop1": "a", "prop2": "b"},...}

Is this possible? Looking at the source code, that would require creating a
LogEvent with extra fields, but it's not clear to me how to inject that
from the Logger.info(...) API.

I was able to achieve something similar creating a custom marker that just
wraps an object. In this way AbstracJsonLayout class will find it (strictly
speaking, the Jackson mapper will) when "getMarker" is written. However
this results in the following:

log.info(new EventMarker(MyObj("a","b")),"alfa");
{.... "marker":{"event":{"prop1":"a","prop2":"b"}, "message":"alfa",....}

Where "event"(*) and "marker" cannot be customised. Is this a right
approach? Should I try something different? I would appreciate any hints,
thank you.

Sebastian

(*) the "event" field is part of my custom EventMarker objects, so this
_could_ be changed dynamically using @JsonAnyGetter and holding a map where
we put(object.class.simpleClassName, object). But I'd like to confirm with
you this is a good approach before moving further...

Re: Adding custom fields dynamically to the JSON layout output.

Posted by Ralph Goers <ra...@dslextreme.com>.
The approach I would use is to implement a JSONMessage class or JSONMapMessage class (which seems reasonable that we should have) and then you would do

log.debug(new JSONMapMessage(map));

JSONMapMessage would override MapMessage’s asString() method to have it return asString(MapFormat.JSON);

Ralph

> On Dec 16, 2015, at 3:29 PM, Sebastian Esponda <se...@gmail.com> wrote:
> 
> Hi,
> 
> When using the JSON layout, we'd like to know if it's possible to log
> objects (instead simple Strings) so that the JSON output includes the
> object itself (serialised by Jackson, which will discover the properties to
> be serialised, adding the fields to the JSON structure accordingly)
> 
> In other words, instead of:
> 
> {...,"message": "some string", ... }
> 
> Something similar this:
> 
> log.debug(MyObj("a","b"))
> {...,"MyObj": { "prop1": "a", "prop2": "b"},...}
> 
> Is this possible? Looking at the source code, that would require creating a
> LogEvent with extra fields, but it's not clear to me how to inject that
> from the Logger.info(...) API.
> 
> I was able to achieve something similar creating a custom marker that just
> wraps an object. In this way AbstracJsonLayout class will find it (strictly
> speaking, the Jackson mapper will) when "getMarker" is written. However
> this results in the following:
> 
> log.info(new EventMarker(MyObj("a","b")),"alfa");
> {.... "marker":{"event":{"prop1":"a","prop2":"b"}, "message":"alfa",....}
> 
> Where "event"(*) and "marker" cannot be customised. Is this a right
> approach? Should I try something different? I would appreciate any hints,
> thank you.
> 
> Sebastian
> 
> (*) the "event" field is part of my custom EventMarker objects, so this
> _could_ be changed dynamically using @JsonAnyGetter and holding a map where
> we put(object.class.simpleClassName, object). But I'd like to confirm with
> you this is a good approach before moving further...



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org