我需要设置系统,该系统可以通过以下方式将所选软件包(MyPck 和 MyPck1)中的 INFO 和 DEBUG 日志级别信息记录到所选文件中:
MyPck DEBUG goes to logFile
MyPck1 INFO goes to logFile
MyPck INFO goes to debugLogFile
MyPck1 DEBUG goes to debugLogFile
我尝试使用 Log4j.properties 中的行来执行此操作:
log4j.logger.MyPck=DEBUG,logFile
log4j.logger.MyPck1=INFO,logFile
log4j.logger.MyPck=INFO,debugLogFile
log4j.logger.MyPck1=DEBUG,debugLogFile
不幸的是 debugLogFile 获取信息并且 logFile 保持为空。如果我按照顺序更改 log4j.properties 中的行 - 首先是 debugLogFile 然后是 logFile 我将 debugLogFile 设置为空。如何解决这个问题呢?
整个 Log4J.properties 设置:
log4j.rootLogger=TRACE, defaultFile
log4j.appender.defaultFile=org.apache.log4j.RollingFileAppender
log4j.appender.defaultFile.File=defaultFile.log
log4j.appender.defaultFile.Threshold=ALL
log4j.appender.defaultFile.MaxFileSize=100MB
log4j.appender.defaultFile.MaxBackupIndex=4
log4j.appender.defaultFile.layout=org.apache.log4j.PatternLayout
log4j.appender.defaultFile.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
log4j.appender.defaultFile.Append=false
log4j.appender.logFile=org.apache.log4j.RollingFileAppender
log4j.appender.logFile.File=logFile.log
log4j.appender.logFile.Threshold=ALL
log4j.appender.logFile.MaxFileSize=100MB
log4j.appender.logFile.MaxBackupIndex=4
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
log4j.appender.logFile.Append=false
log4j.appender.debugLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.debugLogFile.File=debugLogFile.log
log4j.appender.debugLogFile.Threshold=ALL
log4j.appender.debugLogFile.MaxFileSize=100MB
log4j.appender.debugLogFile.MaxBackupIndex=4
log4j.appender.debugLogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLogFile.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
log4j.appender.debugLogFile.Append=false
log4j.logger.MyPck=DEBUG,logFile
log4j.logger.MyPck1=INFO,logFile
log4j.logger.MyPck=INFO,debugLogFile
log4j.logger.MyPck1=DEBUG,debugLogFile
log4j.additivity.MyPck=false
log4j.additivity.MyPck1=false
源代码:
主类:
package tst_log4j;
import org.apache.log4j.Logger;
import MyPck.C;
import MyPck1.C1;
public class Tst_Log4J
{
public final Logger log = Logger.getLogger(getClass());
public static void main(String[] args)
{
C c = new C();
c.doLog();
C1 c1 = new C1();
c1.doLog();
}
}
MyPck 类:
package MyPck;
import org.apache.log4j.Logger;
public class C {
public final Logger log = Logger.getLogger(getClass());
public void doLog()
{
log.info("aaa");
log.debug("bbb");
log.error("ccc");
}
}
MyPck1 类:
package MyPck1;
import org.apache.log4j.Logger;
public class C1 {
public final Logger log = Logger.getLogger(getClass());
public void doLog()
{
log.info("aaa");
log.debug("bbb");
log.error("ccc");
}
}