logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Goers <ralph.go...@dslextreme.com>
Subject Re: Adding custom fields dynamically to the JSON layout output.
Date Wed, 16 Dec 2015 23:43:20 GMT
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 <sesponda@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


Mime
View raw message