禁止将调试日志输出到标准输出

问题描述 投票:0回答:1

我想在日志文件中包含某些类(包 de.martinm.test)的调试日志,但只将错误打印到 STDOUT。 根据我的配置,DEBUG 级别也会打印到 STDOUT

logback.xml:

<configuration>
    <appender name="STDOUT"
       class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%M] %-5level %logger{36} -%kvp- %msg%n</pattern>
        </encoder>
    </appender>
  
    <appender name="FILE-ROTATING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/test.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>logs/test.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <totalSizeCap>20GB</totalSizeCap>
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d %p %c [%M] %m%n</pattern>
        </encoder>
    </appender>
    
    <logger name="de.martinm.test" level="debug">
        <appender-ref ref="FILE-ROTATING"/>
    </logger>
    <root level="error">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
java logback slf4j
1个回答
0
投票

解决方案是在appender中添加一个ThresholdFilter

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
   <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <level>warn</level>
   </filter>
   <encoder>
     <pattern>%d %-5level %logger{36} [%M] %msg%n</pattern>
    </encoder>
  </appender>
© www.soinside.com 2019 - 2024. All rights reserved.