I have the following log4j.properties file:
log4j.rootLogger=DEBUG,A1,A2,A3
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.Threshold=DEBUG
log4j.appender.A1.File=log.out
log4j.appender.A1.MaxFileSize=100KB
log4j.appender.A1.MaxBackupIndex=1
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%p %d %t %c - %m%n
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.Threshold=WARN
log4j.appender.A2.File=warnings.out
log4j.appender.A2.MaxFileSize=100KB
log4j.appender.A2.MaxBackupIndex=1
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%p %d %t %c - %m%n
log4j.appender.A3=org.apache.log4j.RollingFileAppender
log4j.appender.A3.Threshold=ERROR
log4j.appender.A3.File=errors.out
log4j.appender.A3.MaxFileSize=100KB
log4j.appender.A3.MaxBackupIndex=1
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%p %d %t %c - %m%n
In my Java program, I have the following variable defined:
private Logger logger = Logger.getRootLogger();
static {
URL url = MyClass.class.getClassLoader().getResource("/log4j.properties");
if (url == null) {
// log4j.properties not found in CLASSPATH, revert to console output
BasicConfigurator.configure();
}
else {
// log4j.properties found
PropertyConfigurator.configure(url);
}
}
Why is it when I call the logger methods, the output also goes to the console (stdout), in addition to the 3 output files?
If you are going to name your file log4j.properties and put it in the root of your classpath, then it will be picked up by default, you don’t need to pass it to BasicConfigurator.
What is probably happening in your case is that Java’s classloader is not finding the file there (on the root), your call to BasicConfigurator.configure() without parameters is getting log4j to use the default configuration.
Make sure log4j.properties is a the root of your war file’s classpath, i.e.:
WEB-INF\classes\log4j.properties