java - Logging arbitrary objects -
i'm working on logging framework enterprise system , have been looking @ both logback , log4j logging backends trying use slf4j keep backend implementation out of it.
we want log messages out in json format , i've found layout class log4j , encoder logback that. however, have ability add arbitrary key/value pairs json output.
i hoping layout/encoder able detect type of object being logged , in case of map add key/value pairs json else tostring() it.
this doesn't seem possible since slf4j interface seems restricted logging string objects.
i've read on markers, mdc , ndc none of seem fit needs enough.
to clarify, here code snippet optimal:
map m = new hashmap(); m.put("foo", "bar"); m.put("baz", "fluffbunny"); log.info(m);
this output like:
{ "timestamp": "2013-03-04t13:33:40", "foo": "bar", "baz": "fluffbunny" }
or
{ "timestamp": "2013-03-04t13:33:40", "message": { "foo": "bar", "baz": "fluffbunny" } }
there workarounds such rendering json strings message or using mdc around log statements (my layout/encoder classes correctly render mdc , ndc) in both cases clutter log lines.
so question whether there convenient way this. sticking slf4j definite plus perhaps not necessary if benefits of using particular backend outweigh benefits of being backend agnostic.
i have loggablemap
outputs json tostring
, using slf4j
map m = new loggable(); m.put("foo", "bar"); m.put("baz", "fluffbunny"); // note use of {} log arbitrary object rather string log.info("{}", m);
Comments
Post a Comment