Java 配置 logback 以抑制包内所有类的日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24075976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:01:56  来源:igfitidea点击:

Configuring logback to suppress logging from all classes inside a package

javalogginglog4jlogbackspring-boot

提问by San

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

我有这个完美工作的 logback.xml 用于记录所有调试级别语句的控制台。

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

例如,假设我想从属于 org.apache.zookeeper 的类中抑制所有 INFO 级别的日志

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?

我找到的解决方案之一是创建一个自定义过滤器,类似于这里的指示方式 - logback: Two appender, multiple loggers, different levels。但是我真的需要为此编写 java 吗?

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

Thanks in advance!.

提前致谢!。

采纳答案by Dave Syer

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

如果您只有一个 appender(与需要自定义过滤器的链接不同),或者您的所有 appender 都相同,这应该可以工作:

<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 ThresholdFilterin your original was adding anything BTW, and the XML is not valid (there's no <xml/>tag).

我认为ThresholdFilter您的原件中没有添加任何 BTW 内容,并且 XML 无效(没有<xml/>标签)。

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

此外,如果您使用的是 Spring Boot,appender 模式看起来与默认模式非常相似,因此您可以这样做:

<configuration>
  <include resource="org/springframework/boot/logging/logback/basic.xml"/>
  <logger name="org.apache.zookeeper" level="WARN"/>
</configuration>