Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 456
San Avatar asked Jun 06 '14 06:06

San


People also ask

How do I disable Logback logging?

Logback does not allow logging to be disabled from the command line. However, if the configuration file allows it, you can set the level of loggers on the command line via a Java system property.

How do you filter Logback logs?

In logback-classic, filters can be added to Appender instances. By adding one or more filters to an appender, you can filter events by arbitrary criteria, such as the contents of the log message, the contents of the MDC, the time of day or any other part of the logging event.

Does Logback require slf4j?

Note that logback-classic transitively includes the slf4j-api and logback-core , so only having the logback-classic is enough to setup logback with slf4j. When using Logback with SLF4j, a console appender with DEBUG log level is configured automatically.


1 Answers

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>
like image 123
Dave Syer Avatar answered Oct 09 '22 10:10

Dave Syer