[logback-user] custom fields

Ceki Gulcu ceki at qos.ch
Wed Jul 22 11:51:41 CEST 2009


Hello Mel/machintruc,

You can pass data to your custom appender via two SLF4J primitives.

1) via MDC

MDC is handy if you have contextual information which does not change
over several consecutive logger calls. Typical values are userId,
user's ip address, transaction id.

2) via Parameters to the printing methods of a logger

The trace(), debug(), info(), warn() and error() are the printing
methods defined in the org.slf4j.Logger interface. These methods take
parameters which are typically used to avoid parameter concatenation
for disabled log statements (essentially a performance trick). See
http://www.slf4j.org/faq.html#logging_performance for more info.

However, you can also pass data using the parametrized form of
printing methods. For example,

Map map = new HashMap();
map.put("context1", "value1";
map.put("context2", value2");
logger.debug("the sky is blue", map);

Note that the formatting message has no anchor, i.e. the "{}" pair.

You have not explained what the context information consists of. In
the absence of more information, that's the best advice I can give.

Cheers,


Mel T. wrote:
> Hi,
> 
> We have an in-house logger that we want to get rid of. This logger has
> application specific functionality.
> 
> - We store logs in a database table
> 
> - We have specific columns that put some very-frequently used context
> information
> 
> - For that purpose, our logger has methods like this:
>    log(String message, String someContextID1, String someContextID2, ...);
> 
> - This is added to the DB with specific columns for someContextID1,
> someContextID2, etc...
> 
> - We have a log viewer that allows to select logs based on that
> context information.
> 
> So we need an appender that will continue to fill up that same table.
> 
> If we move to a logger like logback, I am unsure how to best handle
> our application need.
> 
> This first comes to mind:
> 
> - Create our own facade that adds the specific log methods (like above)
> - This facade assembles these arguments into a parsable message given
> to sfl4j/logback
> - An in-house DBAppender can parse the message to extract the
> arguments and store them in the right column.
> 
> Or is there a more natural way to do that?
> 
> I looked at markers, MDC, but is is not clear to me if any of them
> would be good for that. Maybe MDC could be used that way:
> 
> - Create our own facade that adds the specific log methods (like above)
> - This facade assembles these arguments and places them in MDC
> - An in-house DBAppender can read the MDC and store values in the right column.
> 
> Also, is'nt adding another facade on top of SLF4J/logback a little too
> much? Since we may add out own facade, should'nt we directly use
> logback?
> 
> --
> Mel T.
> 

-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch


More information about the Logback-user mailing list