Logback忽略父包的记录器

时间:2015-03-11 13:37:21

标签: java logging logback

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.testmy.test记录器的消息收集并打印包my.test的所有 INFO 消息并向下?

1 个答案:

答案 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>