Class loggin to multiple log files with log4j

落爺英雄遲暮 提交于 2019-12-11 09:29:24

问题


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



来源:https://stackoverflow.com/questions/4749015/class-loggin-to-multiple-log-files-with-log4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!