Class loggin to multiple log files with log4j

2019-08-12 14:21发布

问题:

I want my application to log to two files: first one, already exists, and will log everything. The second one only will log conditionally.

This is my log4j.properties:

log4j.rootCategory=DEBUG, FILE

log4j.logger.es.vf.pegaso=DEBBUG
log4j.logger.org=ERROR
log4j.logger.org.hibernate=ERROR
log4j.logger.org.displaytag=ERROR
log4j.logger.es.vf.pegaso.common.form.el.ElEvaluator=ERROR
log4j.logger.es.vf.pegaso.common.util.csvreader.CsvReaderElEvaluator=ERROR

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.file=log/test.log
log4j.appender.FILE.Threshold=DEBUG
log4j.appender.FILE.MaxFileSize=3MB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS}\t%t\t%-5p\t[%c{1}]\t%m%n

To create the log I do it like this:

private static final Logger logger = Logger.getLogger(*className*.class);

I tried this but it didn't seem to worked:

log4j.category.App=FILE2

log4j.appender.FILE2=org.apache.log4j.FileAppender
log4j.appender.FILE2.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE2.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} - %m%n
log4j.appender.FILE2.file=application.log

To call the second logger I do it like this:

 private static final Logger applogger = Logger.getLogger("App");

Can anyone point where's my mistake.

Thanks in advance and best regards.

回答1:

try this:

log4j.category.APP=DEBUG, FILE2
log4j.appender.FILE2.threshold=DEBUG
log4j.appender.FILE2=org.apache.log4j.FileAppender
log4j.appender.FILE2.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE2.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} - %m%n
log4j.appender.FILE2.file=application.log


回答2:

Maybe you have to add the second logger... like
log4j.logger.your.package=FILE2