今天和两个同事讨论Log4j,他们都需要解决一个问题,怎么分开输出Logger。这么讲不清楚,举个例子:
package com.gmail.at.ankyhe.log4jtest; import org.apache.log4j.Logger; public class ClassA { private static Logger logger = Logger.getLogger(ClassA.class.getName()); public ClassA() { logger.info("ENTER ClassA()"); } public void foo() { logger.info("foo()"); bar(); } public void bar() { Logger myLog = Logger.getLogger("bar"); myLog.debug("bar()D"); myLog.info("bar()I"); } }
我希望bar可以输出到一个地方,其他的logger可以输出到一个地方。一般的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <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{dd HH:mm:ss,SSS} %-5p] [%t] %c{2} - %m%n" /> </layout> </appender> <appender name="myFile" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="xml.log" /> <param name="Append" value="false" /> <param name="MaxBackupIndex" value="10" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p (%c:%L)- %m%n" /> </layout> </appender> <logger name="bar"> <level value="info" /> <appender-ref ref="myConsole" /> </logger> <root> <priority value="debug" /> <appender-ref ref="myFile" /> </root> </log4j:configuration>
这样会发现一个问题,myLog的输出又会在文件,又会在终端。原因在Log4J官方的Introduction讲的很清楚:
http://logging.apache.org/log4j/1.2/manual.html 搜索Appender Additivity. 我解释一下就是默认情况下bar会集成root的输出。知道了原因,那怎么修改就简单了。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <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{dd HH:mm:ss,SSS} %-5p] [%t] %c{2} - %m%n" /> </layout> </appender> <appender name="myFile" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="xml.log" /> <param name="Append" value="false" /> <param name="MaxBackupIndex" value="10" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p (%c:%L)- %m%n" /> </layout> </appender> <logger name="bar" additivity="false"> <level value="info" /> <appender-ref ref="myConsole" /> </logger> <root> <priority value="debug" /> <appender-ref ref="myFile" /> </root> </log4j:configuration>
唯一的区别是在bar那个logger后面加了一个 additivity="false"。我试了一下,可以按照要求工作。这是xml的配置,一般的property的配置如下:
log4j.rootLogger=DEBUG, FA log4j.category.bar = INFO, CA log4j.additivity.bar = false #Console Appender log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n #File Appender log4j.appender.FA=org.apache.log4j.FileAppender log4j.appender.FA.File=property.log log4j.appender.FA.layout=org.apache.log4j.PatternLayout log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n