30

I have this perfectly working logback.xml for console which logs all the debug level statements.

<?xml version="1.0" encoding="UTF-8"?>
  <configuration>
    <property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
      <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>DEBUG</level>
      </filter>
      <encoder>
        <pattern>${CONSOLE_LOG_PATTERN}</pattern>
      </encoder>
    </appender>
    <root level="DEBUG">
      <appender-ref ref="CONSOLE"/>
    </root>
  </configuration>
</xml>

Now I'd like modify this to suppresses logging from all the loggers from a certain package.

For instance, say I'd like to suppress all the INFO level logs from from classes that belongs to org.apache.zookeeper

One of the solutions I found was by creating a custom filter, similar to how it's indicated here - logback: Two appenders, multiple loggers, different levels . But do I really need to write java for that?

Comparing this problem to log4j, this can be easily accomplished by following  - 
log4j.logger.org.apache.zookeeper=WARN, CONSOLE

Thanks in advance!.

Community
  • 1
  • 1
San
  • 5,051
  • 3
  • 16
  • 12

1 Answers1

36

If you only have one appender (unlike in your link that needed a custom filter), or all your appenders are the same, this should work:

<configuration>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>...</pattern>
    </encoder>
  </appender>
  <root level="DEBUG">
    <appender-ref ref="CONSOLE"/>
  </root>
  <logger name="org.apache.zookeeper" level="WARN"/>
</configuration>

I don't think the ThresholdFilter in your original was adding anything BTW, and the XML is not valid (there's no <xml/> tag).

Also, if you're using Spring Boot, the appender pattern looks very similar to the default, so you can probably just do this:

<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml"/>
  <logger name="org.apache.zookeeper" level="WARN"/>
</configuration>
Helder Pereira
  • 5,522
  • 2
  • 35
  • 52
Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • Is there a way to avoid all levels of logging from package `org.apache.zookeeper` in the configuration, other than creating a custom `Filter` – Manu Jan 20 '16 at 14:08
  • 10
    `level="OFF"` should do it. Even better, you can do it in your `application.properties` with `logging.level.org.apache.zookeeper=OFF` (the `application.properties` bit wasn't available when I wrote the original answer) – Dave Syer Jan 20 '16 at 22:04
  • 1
    The `` line helped me. – akauppi Mar 03 '19 at 18:32