Logback忽略父包的记录器。
我有那些追加者:
附加器
<appender name="fileAppender1" class="ch.qos.logback.core.FileAppender">
<file>myLogFile1.log</file>
<encoder>
<pattern>%d [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="fileAppender2" class="ch.qos.logback.core.FileAppender">
<file>myLogFile2.log</file>
<encoder>
<pattern>%d [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
还有3个记录器:
记录器1
<logger name="my" level="INFO" additivity="false" >
<appender-ref ref="fileAppender1" />
</logger>
记录器2
<logger name="my.test" level="DEBUG" additivity="false" >
<appender-ref ref="console" />
</logger>
记录器3
<root level="TRACE">
<appender-ref ref="fileAppender2" />
</root>
additivity="false"
所有记录器都写相同级别的相同信息(最高 - 信息) 如何配置 log root,log logger通过appender收集并打印所有 TRACE 日志消息,my
记录器收集并打印所有 DEBUG 包my
没有包my.test
和my.test
记录器的消息收集并打印包my.test
的所有 INFO 消息并向下?
答案 0 :(得分:0)
使用级别过滤器
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
因此你的logback.xml将成为
<appender name="fileAppender1" class="ch.qos.logback.core.FileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>myLogFile1.log</file>
<encoder>
<pattern>%d [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="fileAppender2" class="ch.qos.logback.core.FileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>TRACE</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>myLogFile2.log</file>
<encoder>
<pattern>%d [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<logger name="my" level="INFO" additivity="false" >
<appender-ref ref="fileAppender1" />
</logger>
<logger name="my.test" level="DEBUG" additivity="false" >
<appender-ref ref="console" />
</logger>
<root level="TRACE">
<appender-ref ref="fileAppender2" />
</root>
<强>更新强>
如果你需要ERROR和WARN使用带有WARN的ThresholdFilter(如下所示),如果你需要信息,错误和警告使用阈值过滤器作为信息(我相信默认行为)
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>