1
votes

I'm using SLF4J and LogBack in my application. I'm using several 3rd party libraries that also use SLF4J. In order to hide their logs, I've set the root log level to "WARN". However, for my own logger, I want to display everything (regardless of the message level) in my log output. Here's my config so far:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>[%level] %date{YYYY-MM-dd HH:mm:ss} [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>

  <!-- For all loggers, use WARN as minimum level to avoid log overflow -->
  <root level="WARN">
     <appender-ref ref="STDOUT"  />
  </root>

  <!-- For the specific "com.example.MyLogger.java", use log level DEBUG -->
  <logger name="MyLogger" level="DEBUG">
     <appender-ref ref="STDOUT" />
  </logger>

  <consolePlugin />
</configuration>

Unfortunately, the configuration displayed above does not work for me as intended. I only get messages from the "MyLogger" class if they have a log level of WARN or above.

So to recap what I want:

  • Generally, only messages with level WARN or above should be logged...
  • ... except for one specific logger class, where everything should get logged regardless of level

Is this possible? How do I do that?

Thanks,

Alan

EDIT: I've found a solution myself after experimenting some more. Here's the logback.xml:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>[%level] %date{YYYY-MM-dd HH:mm:ss} [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>


  <logger name="com.example" level="ALL">
    <appender-ref ref="STDOUT" />
  </logger>

  <root level="WARN">
    <appender-ref ref="STDOUT"  />
  </root>

  <consolePlugin />
</configuration>

Note that the first logger does not put a limit on the concrete logger class, but rather on the prefix of the qualified class name. So everything coming from com.example.* will be logged. Since there is one specific logger class in my application, that amounts to the same result as intended.

1

1 Answers

2
votes

You should mention the package which you want to change the log level not just the class

<logger name="com.example.MyLogger" level="DEBUG" />

Refer the logack documentation