Setting package wide logger level via Logger

2019-08-16 05:14发布

I need to setup system that could log INFO and DEBUG log level information from selected packages (MyPck and MyPck1) to selected files in following way:

MyPck DEBUG goes to logFile
MyPck1 INFO goes to logFile

MyPck   INFO goes to debugLogFile
MyPck1 DEBUG goes to debugLogFile

I try to do this with lines in Log4j.properties:

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

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

Unfortuetly debugLogFile gets info and logFile remains empty. In case if I change lines in log4j.properties according order - first debugLogFile then logFile I will have debugLogFile empty. How to solve this problem?

Whole Log4J.properties settings:

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

SOURCE CODE:

Main class:

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 class:

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 class:

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");

    }

}

标签: java log4j
2条回答
贼婆χ
2楼-- · 2019-08-16 05:45

You could create one appender for each scenario and the log out to both.

Give this a go:

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
查看更多
对你真心纯属浪费
3楼-- · 2019-08-16 05:47

You can't assign two log thresholds to a logger.

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

is exactly the same as

log4j.logger.MyPck=INFO,debugLogFile

i.e. the first assignment is overwritten. To get output in both log files, you need:

log4j.logger.MyPck=DEBUG,logFile,debugLogFile
查看更多
登录 后发表回答