[logback-user] Structured Logging with logback

Moebius.Jonathan at swm.de Moebius.Jonathan at swm.de
Wed Nov 14 16:15:24 CET 2012

Hello List,

I would like to be able to automatically relate log-messages of different executions of my application. Goal of the exercise is to automatically detect certain tendencies within the application - for example detecting that fluctuating values are slowly but steadily approaching critical barriers. To do so, I would like to employ two appenders, one file-appender for a superficial check whether the application run ok and one (automatic) in depth analysis whether there are any problems lingering beyond the horizon.

My initial idea was to employ MDCs for this, however, that seems rather odd, because MDCs are bound to the log-invoking thread and thus my code would look a lot like 

MDC.put(key, value);

which can't be right.

So my idea was to enforce a certain pattern in the logmessages, say <message>::<key>=<value>;<key>=<value>... . With this format (and given a custom db-appender) messages could be written into a database in a way were the intended datamining is possible.

Next comes the idea, how to force the application developer to adhere to this pattern? Ideally, there would be a Logger.info(msg, param[]) method, where the params are appended to the message not according to the curly braces but according to the abovementioned pattern. This method - of course - does not exist. 

I thought about writing a Logger-wrapper which enforces abovementioned message-format, but that would ruin the method/line detection of Class CallerData, as the FQCN property in the Logger cannot be set from the outside. So I dug out the big irons and tried to inherit from ch.qos.logback.classic.Logger, which does not work, because this class is final. Comes the even bigger iron of reflection... You can see that this is heading in a very wrong direction.

So, am I really the only one trying to log auto-analysable data or did I miss something out? Is it even something which could/should be included in the standard? Could/should I simply abuse the param-array of the LoggingEvent Object in a custom appender, instead of using LoggingEvent.getFormattedMessage()?

Kind regards,

More information about the Logback-user mailing list