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");
}
}
You could create one appender for each scenario and the log out to both.
Give this a go:
You can't assign two log thresholds to a logger.
is exactly the same as
i.e. the first assignment is overwritten. To get output in both log files, you need: