logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sebastian Esponda <sespo...@gmail.com>
Subject Adding custom fields dynamically to the JSON layout output.
Date Wed, 16 Dec 2015 22:29:41 GMT
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...

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message