[logback-user] SocketAppender application property

Ceki Gulcu ceki at qos.ch
Wed Aug 19 16:41:53 CEST 2009


Hello Hauke,


For application-wide values, you don't need to use MDC. Logback offers
a better suited alternative in the form of context properties. Every
logger is attached to a context, you can obtain it as:

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

you can then write:

lc.putProperty("key", "val");

Context properties can also be set via configuration files. See
http://logback.qos.ch/manual/configuration.html#variableSubstitution

Moreover, every logging event generated by logback will contain the
properties contained in the context, i.e. they are available to all
appenders. Context information is also serialized with each outgoing
logging event.

If you are only interested in setting the name of the application, you
can set the name of the context in the configuration file. See
http://logback.qos.ch/manual/configuration.html#contextName

The %contextName conversion word in PatternLayout will output the name
of the logger context.

I think the above meets the requirements you mentioned, doesn't it?

HTH,

haukex wrote:
> Hi Ralph,
> 
> Thanks for the information, I was also thinking about writing some custom
> code to insert the value in the MDC myself, but I was hoping this might be
> supported out-of-the-box :) Since the value really is application-wide, it
> makes more sense to me as something to be configured in logback.xml... I was
> thinking of writing a custom filter that does nothing but set this value in
> the MDC, but that seems a little wasteful.
> 
> Regards,
> -- Hauke D
> 
> 
> rgoers wrote:
>> In my organization we use a mechanism very similar to that shown in
>> http://www.slf4j.org/extensions.html#event_logger 
>> . SLF4J doesn't provide standard key names or methods for common  
>> fields, but you can easily create a wrapper of the MDC to do this. We  
>> have a class called RequestContext that does exactly that. All our  
>> applications use that class to access the data in the MDC.
>>
>> Ralph
>>
> 

-- 
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