如何在回滚时将子包日志移动到不同的日志文件?

时间:2017-10-28 14:29:39

标签: java logging log4j logback

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="appLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<File>${appLogFile}</File>
<encoder>
   <Pattern>%d{dd-MM-yy HH:mm:ss.SSS}\t%X{reqId}\t%thread\t%level\t%logger{36}\t%M\t%line\t%msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
   <fileNamePattern>${appLogFile}.%i</fileNamePattern>
   <minIndex>1</minIndex>
   <maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
   <maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
</appender>

<appender name="REPORT-XYZ-LOG-APPENDER"
class="ch.qos.logback.core.rolling.RollingFileAppender">

<file>${appLogFile}.reports_xyz.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  <Pattern>%d{dd-MM-yy HH:mm:ss.SSS}\t%X{reqId}\t%23thread\t%-5level\t%-38logger{36}\t%-25M\t%4line\t%msg%n</Pattern>
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  <!-- rollover daily -->
  <fileNamePattern>${appLogFile}.reports_xyz.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
  <timeBasedFileNamingAndTriggeringPolicy   class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    <maxFileSize>10MB</maxFileSize>
  </timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>

<appender name="REPORT-LOG-APPENDER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${appLogFile}.reports.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  <Pattern>
            %d{dd-MM-yy HH:mm:ss.SSS}\t%X{reqId}\t%thread\t%level\t%logger{36}\t%M\t%line\t%msg%n
        </Pattern>
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  <!-- rollover daily -->
  <fileNamePattern>${appLogFile}.reports.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
  <timeBasedFileNamingAndTriggeringPolicy   class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    <maxFileSize>10MB</maxFileSize>
  </timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>

</appender>

<logger name="com.xyz.a.b.c" level="INFO" additivity="false">
<appender-ref ref="REPORT-XYZ-LOG-APPENDER" />
</logger>

<logger name="com.xyz" level="INFO" additivity="false">
  <appender-ref ref="appLog"/>
</logger>

<root level="INFO">
<appender-ref ref="REPORT-LOG-APPENDER" />
</root>

</configuration>

我想将包“com.xyz.a.b.c”的日志移动到不同的文件,而不是包“com.xyz”日志的位置。但是在使用上面的logback.xml文件之后,对于父包和子包,日志仍然是$ {appLogFile}(appLogFile是系统属性并具有绝对文件路径)。有人可以帮助我,我在这里做错了吗?

我想移动com.xyz.a.b.c的日志 - &gt;文件1                         com.xyz - &gt; file2的

1 个答案:

答案 0 :(得分:0)

我得到了答案。这只是我的错误。更改了logback.xml文件以将其移动