Class loggin to multiple log files with log4j

2019-08-12 13:56发布

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.

2条回答
可以哭但决不认输i
2楼-- · 2019-08-12 14:18

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

查看更多
虎瘦雄心在
3楼-- · 2019-08-12 14:34

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
查看更多
登录 后发表回答