的logback不同的文件不同层次(Logback different files for diff

2019-07-03 16:28发布

我有这样的附加目的地在我logback.xml

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logFile.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern>
        <maxHistory>5</maxHistory>
    </rollingPolicy>

    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg%n</Pattern>
    </layout>
</appender> 

<root>
    <level value="ALL" />
    <appender-ref ref="FILE" />
</root>

因此,在此刻,我是我所有的日志保存到一个文件中。 我怎样才能使它像,一个文件只保存错误日志等保存所有其他的?

我希望只使用1个记录器的实例在我的代码,像这样保持:

 private static final Logger log = LoggerFactory.getLogger(Main.class);

Answer 1:

开始寻找的logback类别,发现过滤器。

只需添加过滤器描述您的appender:

    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERROR</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>


Answer 2:

这是我对于JBoss做,但我猜你会得到解决。

对于error.log中创建一个专用的appender如下,

<appender name="ErrorFile" class="org.jboss.logging.appender.DailyRollingFileAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="File" value="${jboss.server.log.dir}/error.log"/>
      <param name="Threshold" value="ERROR"/>
      <param name="Append" value="false"/>

      <!-- Rollover at midnight each day -->
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>

      <!-- Rollover at the top of each hour
      <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
      -->

      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>

         <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
         <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
          -->
      </layout>
   </appender>

对于其余的人创建一个appender

 <appender name="File" class="org.jboss.logging.appender.DailyRollingFileAppender">
          <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
          <param name="File" value="${jboss.server.log.dir}/server.log"/>
          <param name="Threshold" value="ALL"/>
          <param name="Append" value="false"/>

          <!-- Rollover at midnight each day -->
          <param name="DatePattern" value="'.'yyyy-MM-dd"/>

          <!-- Rollover at the top of each hour
          <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
          -->

          <layout class="org.apache.log4j.PatternLayout">
             <!-- The default pattern: Date Priority [Category] Message\n -->
             <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>

             <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
             <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
              -->
          </layout>
       </appender>

然后,创建特定类别为上述两种

<category name="your_file_name/package_name">
      <priority value="ERROR"/>
      <appender-ref ref="ErrorFILE"/>
 </category>
 <category name="your_file_name/package_name">
      <priority value="ALL"/>
      <appender-ref ref="File"/>
 </category>


文章来源: Logback different files for different levels