Log4j Logging to multiple files

2019-02-15 11:44发布

In my Java console app, I want to log certain events to a log file and certain others to console. This is what I've got now

log4j.rootLogger=error, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


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

In my Java app, I instantiate two log instances using

private static Logger log = Logger.getLogger(ActiveDirectoryManage.class);
private static Logger failedToAddUsersLogger = Logger.getLogger("FailedToAddUsersLogging");

My issue is that failedToAddUsersLogger.warn("xyz") also writes to the console in addition to the log file failedtoaddusers.log. I just want it to write to the log file and not to the console. How do I accomplish that?

1条回答
看我几分像从前
2楼-- · 2019-02-15 12:23

You need to set additivity to "false" (read more on logger additivity in the Appenders and Layouts section of the log4j manual):

log4j.additivity.FailedToAddUsersLogging=false
log4j.logger.FailedToAddUsersLogging = your level, L1

Also, make sure you have one of new versions of log4j, "additivity" setting was not available from beginning.

查看更多
登录 后发表回答