Search code examples
javalogginglog4jlogbackspring-boot

Configuring logback to suppress logging from all classes inside a package


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


Solution

  • 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>