How to create 2 different ROOT loggers with logback?

Dimitri Dewaele picture Dimitri Dewaele · Sep 16, 2013 · Viewed 30.5k times · Source

I'm happily using SLF4J with logback and use 2 appenders for the ROOT logger.

<root level="DEBUG">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
</root>

How could we have different log-levels for both appenders? I still need all ROOT-logger messages.

  • DEBUG-level for STDOUT
  • INFO-level for FILE

All log's need to be part of the output (so the ROOT logger is needed).

Answer

sheltem picture sheltem · Sep 16, 2013

You will not ever have more than one root-logger, so your question is a bit misleading. What you are looking for is how to fine-tune which events each of the appenders does log.

And for that, you add a ThresholdFilter to each of the appenders:

http://logback.qos.ch/manual/filters.html#thresholdFilter

<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  <level>INFO</level>
</filter>

Configure level INFO for the FILE-appender and DEBUG for STDOUT.

Edit: I have to dispute the other answer's charge that this one is wrong: Yes, you can have more than one root-element in the configuration. That does not create more than one root-logger though, which was what the question's title asks for. Also, the logback manual states under http://logback.qos.ch/manual/configuration.html#syntax (highlighting mine):

Nevertheless, the very basic structure of the configuration file can be described as, < configuration > element, followed by zero or more < appender > elements, followed by zero or more < logger > elements, followed by at most one < root > element.

It might work, but at the very least it's against convention.