54

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127

2 Answers2

63

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.

sheltem
  • 3,754
  • 1
  • 34
  • 39
  • I have ` ` and it is still logging in both the appenders configured, how is it possible? – Govinda Sakhare Aug 11 '16 at 07:06
  • I doubt anyone could help you with as little information as that. If you have a problem to solve, please open a new question, comments are not the place for this. – sheltem Aug 11 '16 at 08:04
  • It does appear to work, but not only the documentation but also the code shows support for only one root logger, so I suspect some of the functionality of the root logger is being missed. E.g. if you declare 2 root loggers and one has a different level, what is it the default level? Note how the [source code](https://github.com/qos-ch/logback/blob/master/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java) creates and initializes a _single_ root logger in the constructor. – Rhubarb Jul 31 '17 at 18:35
  • 1
    @Rhubarb the default level will be the last one set. See the maintainer's answer: https://stackoverflow.com/questions/41647036/multiple-root-loggers-with-logback-conditionals – ibai Sep 14 '17 at 13:44
  • logback has inheritance'ish based on packages...but not by level, at all... – rogerdpack Dec 19 '19 at 16:12
1

You can have multiple root elements, each with an associated logging level and an appender-ref (I'm working with logback.version>1.0.13) In this case you also have to put a FILTER inside yours appenders, like that:

<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
    <resetJUL>true</resetJUL>
</contextListener>

<!-- To enable JMX Management -->
<jmxConfigurator/>

<appender name="console-info" class="ch.qos.logback.core.ConsoleAppender">
   <filter class="ch.qos.logback.classic.filter.LevelFilter">
     <level>INFO</level>
     <onMatch>ACCEPT</onMatch>
     <onMismatch>DENY</onMismatch>
  </filter>
  <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
  </encoder>
</appender>  

<appender name="console-debug" class="ch.qos.logback.core.ConsoleAppender">
  <filter class="ch.qos.logback.classic.filter.LevelFilter">
     <level>DEBUG</level>
     <onMatch>ACCEPT</onMatch>
     <onMismatch>DENY</onMismatch>
  </filter>
  <encoder>
      <pattern>[%d{ddMMMyyyy HH:mm:ss.SS}]%-5level %logger{45} - %msg %n</pattern>
  </encoder>
</appender>


<root level="info">
    <appender-ref ref="console-info"/>
</root>
<root level="debug">
    <appender-ref ref="console-debug"/>
</root>

m.piunti
  • 340
  • 2
  • 8
  • 13
    Well that goes against the documentation: http://logback.qos.ch/manual/configuration.html#syntax .. " at most one element" – Matt Byrne May 25 '15 at 22:29
  • 1
    Your configuration is misleading. Your `console-debug` logs only and only `debug` level (e.g., not `warn`) and analogically `console-info` logs only `info` level. – Stepan Vavra Nov 05 '15 at 10:43
  • 1
    With double root definition do appenders accumulate? I think according to manual "info", "warn" & "error" level messages will go into both logs (assuming filter is not set for appenders). Am I right? – Simon Logic Oct 22 '17 at 20:24
  • Multiple root tag doesn't give any error. But, it does not work as expected. The second appender receives info messages as well. Only the previous answer is right. – Karthik Sankar Oct 11 '19 at 20:51