我需要设置系统,可以通过以下方式将选定包(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");
}
}
答案 0 :(得分:0)
您无法为记录器分配两个日志阈值。
log4j.logger.MyPck=DEBUG,logFile
log4j.logger.MyPck=INFO,debugLogFile
与
完全相同log4j.logger.MyPck=INFO,debugLogFile
即。第一个作业被覆盖。要在两个日志文件中获得输出,您需要:
log4j.logger.MyPck=DEBUG,logFile,debugLogFile
答案 1 :(得分:0)
您可以为每个方案创建一个appender,并注销两个方案。
放手一搏:
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.myPckDebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.myPckDebugAppender.File=logFile.log
log4j.appender.myPckDebugAppender.Threshold=DEBUG
log4j.appender.myPckDebugAppender.MaxFileSize=100MB
log4j.appender.myPckDebugAppender.MaxBackupIndex=4
log4j.appender.myPckDebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myPckDebugAppender.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
log4j.appender.myPckDebugAppender.Append=false
log4j.appender.myPckInfoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.myPckInfoAppender.File=debugLogFile.log
log4j.appender.myPckInfoAppender.Threshold=INFO
log4j.appender.myPckInfoAppender.MaxFileSize=100MB
log4j.appender.myPckInfoAppender.MaxBackupIndex=4
log4j.appender.myPckInfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myPckInfoAppender.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
log4j.appender.myPckInfoAppender.Append=false
log4j.appender.myPck1DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.myPck1DebugAppender.File=debugLogFile.log
log4j.appender.myPck1DebugAppender.Threshold=DEBUG
log4j.appender.myPck1DebugAppender.MaxFileSize=100MB
log4j.appender.myPck1DebugAppender.MaxBackupIndex=4
log4j.appender.myPck1DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myPck1DebugAppender.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
log4j.appender.myPck1DebugAppender.Append=false
log4j.appender.myPck1InfoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.myPck1InfoAppender.File=logFile.log
log4j.appender.myPck1InfoAppender.Threshold=INFO
log4j.appender.myPck1InfoAppender.MaxFileSize=100MB
log4j.appender.myPck1InfoAppender.MaxBackupIndex=4
log4j.appender.myPck1InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myPck1InfoAppender.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
log4j.appender.myPck1InfoAppender.Append=false
log4j.logger.MyPck=DEBUG,myPckInfoAppender,myPckDebugAppender
log4j.logger.MyPck1=DEBUG,myPck1InfoAppender,myPck1DebugAppender
log4j.additivity.MyPck=false
log4j.additivity.MyPck1=false