Logging different log levels to different appenders with logback

Assume a scenario where you want debug messages to be logged a different file than the main one that captures messages above info log level. The way to configure this is quite different especially when you come from a log4j background.

Having log4j knowledge one would start with 


   <appender name="FILE"

   <appender name="darshika, d. Shah;

   <logger name="mylogger">
     <level value="debug" />
     <appender-ref ref="FILE" /gut;

     <level value="error" />
     <appender-ref ref="STDOUT" />

The issue with this is mylogger will output at debug level to both STDOUT and FILE. This is due to the way logback is designed. Pasting it from the original extract

“Contrary to log4j, logback appenders do not automatically inherit a threshold
filter. You probably don’t care but this has to do with the fact that the
functionality offered by log4j is assumed by two separate modules, logback-core
and logback-classic. Logback-core, where several appenders (e.g. FileAppender)
are defined, does not really know about logging. Both the Logger and Level
classes are defined in logback-classic and not in logback-core. In order to
avoid duplicated code, FileAppender is defined only in logback-core and not in
logback-classic. It’s LoggingEvent specific layouts and filters defined in
logback-classic that give a FileAppender instance its “logging-aware” behavior.

Anyway, to cut a long story short, it could have been possible to redefine
FileAppender in logback-classic in order to add a threshold-filter. But as it is
possible to accomplish the same by setting a “c.q.l.clas.filter.ThresholdFilter”
in a configuration file, we traded shorter configuration syntax in favor of not
not duplicating code.”

So the correct solution would be use a ThresholdFilter from the logback filters package. Here is how the configuration file would look like


   &lt;appender name=&quot;FILE&quot;&gt;
        &lt;filter class=&quot;ch.qos.logback.classic.filter.

   &lt;appender name=&quot;STDOUT&quot;&gt;
        &lt;filter class=&quot;ch.qos.logback.classic.filter.

     &lt;level value=&quot;DEBUG&quot; /&gt;
     &lt;appender-ref ref=&quot;STDOUT&quot; /&gt;
     &lt;appender-ref ref=&quot;FILE&quot; /&gt;

Note that the root log level is now set to DEBUG i.e. specify a lowest log level accepted to log in the root element.  A threshold filter is used to filter events below the specified log level.


Additionally if one wants that only messages with a specified severity (log level) to be logged, a LevelFilter should be used. Assuming only debug messages are to be logged to the file and all messages above info log level should go to the console, the configuration file will now look like


   <appender name="FILE">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">

   <appender name="STDOUT">
       <filter class="ch.qos.logback.classic.filter.ThresholdFilter"gut;

     <level value="DEBUG" /gut;
     <appender-ref ref="
; /> <appender-ref ref="FILE" /> </root> </configuration> [/source code

For more complex filteringonly we can create a custom filter by extending Filter class from logback-core module. Happy coding !


Posted on September 27, 2014, in logback and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: