0

我需要设置系统,该系统可以通过以下方式将所选软件包(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");

    }

}
4

2 回答 2

0

您不能将两个日志阈值分配给一个记录器。

log4j.logger.MyPck=DEBUG,logFile
log4j.logger.MyPck=INFO,debugLogFile

完全一样

log4j.logger.MyPck=INFO,debugLogFile

即第一个作业被覆盖。要在两个日志文件中获取输出,您需要:

log4j.logger.MyPck=DEBUG,logFile,debugLogFile
于 2013-10-29T12:37:13.560 回答
0

您可以为每个场景创建一个附加程序并注销两者。

试一试:

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
于 2013-10-29T12:46:16.547 回答