Issue with log4j log not writing to file

2019-07-11 12:06发布

问题:

Does anyone see why this log is not writing to file. It is writing to standard out twice but not to the file:

Also, I tried removing the "Stdout" appender and then I don't get any logging at all.

package org.berlin.wicket;
import org.apache.log4j.Logger;
private static final Logger LOG = Logger.getLogger(QuickstartPage.class);
LOG.info("Loading constructor");

log4j.rootLogger=DEBUG,Stdout,mainAppender

log4j.appender.Stdout=org.apache.log4j.ConsoleAppender log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n

log4j.appender.mainAppender=org.apache.log4j.DailyRollingFileAppender

log4j.appender.mainAppender.file=logs/core.log

log4j.appender.mainAppender.datePattern='.'yyyy-MM-dd

log4j.appender.mainAppender.append=true

log4j.appender.mainAppender.layout=org.apache.log4j.PatternLayout

log4j.appender.mainAppender.layout.ConversionPattern=[%d{MM/dd/yyyy HH:mm:ss.SSS}] [%C{1}.%M():%L] [%t] [%p] - %m%n

log4j.logger.org.berlin=DEBUG,Stdout,mainAppender

回答1:

You have two loggers which use the same appender (Stdout), hence why you see entries on the console twice.

As others have mentioned, the properties are case-sensitive, hence why your file appender is not configured correctly.



回答2:

The File and Append properties are case senstive.

log4j.appender.mainAppender.File=someFileName.log log4j.appender.mainAppender.Append=true



回答3:

You should create an instance of FileHandler that writes log to a file called myfile.log.

FileHandler fileHandler = new FileHandler("myfile.log", true);        
logger.addHandler(fileHandler);