log4j有针对性的将日志输出到不同文件且不重复输出到root中

需求描述:将消费的队列情况单独生成到一个日志文件中,防止消息丢失找回。
做如下log4j配置




<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

     <appender name="myConsole" class="org.apache.log4j.ConsoleAppender">
           <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="[%d{MM-dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
           layout>
           <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="levelMin" value="info" />
                <param name="levelMax" value="off" />
                <param name="AcceptOnMatch" value="true" />
           filter>
     appender>

     <appender name="activexAppender" class="org.apache.log4j.DailyRollingFileAppender">
           <param name="File" value="../log/test.log" />
           <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
           <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern"
                     value="[%d{MM-dd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />
           layout>
     appender>

     
     <appender name="queueAppender" class="org.apache.log4j.DailyRollingFileAppender">
           <param name="File" value="../log/myTest.log" />
           <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
           <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="[%d{MM-dd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />
           layout>
     appender>
     <logger name="queueLog" additivity="false">
       <level value="info"/>
       <appender-ref ref="queueAppender"/>
     logger>
     
     <root>
           <priority value="info" />
           <appender-ref ref="myConsole" />
           <appender-ref ref="activexAppender" />
     root>
log4j:configuration>

logger 为不同的文件使用不同的log配置 additivity=”false”表示不继承父logger的配置,这个log不会将日志反馈到root中:

private static Log logger = LogFactory.getLog(ConsumeQueue.class);
     //用于单独记录消费队列
private static Log queueLog = LogFactory.getLog("queueLog");

你可能感兴趣的:(log4j有针对性的将日志输出到不同文件且不重复输出到root中)