开发者

Configuring multiple log files in log4j while using categories

Here's what I'm trying to do: I want 2 log files: The first logs INFO level and up for all parts of the applications but also logs DEBUG and up for some packages. The second only logs ERROR and up across all packages. I'm sure this is probably trivial but I can't quite figure it out. Here is the configuration file I am currently using:

log4j.rootLogger=INFO,console,R

#console appender
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %t %-5p %c{2} - %m%n

#file appender
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern = '.'yyyy-MM-dd
log4j.appender.R.File = log/log.t开发者_JS百科xt
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = [%d{ISO8601}]%5p%6.6r[%t]%x(%F:%L) - %m%n

#Specific log levels
log4j.category.com.mypackage1=DEBUG
log4j.category.com.mypackage2=DEBUG

This doesn't have the error log part, obviously. My basic idea was to add another appender and set its log level to ERROR but the categories seem to override it and and their info in as well, which is not what I want. The reason they are there is because the other packages dump alot of information that we don't need when set to debug and this is how we are getting around it. I'm thinking there may be a better overall approach to this but this is my first log4j config file ever.

UPDATE: A co-worker suggested using 2 loggers as is mentioned in the post that kdgregory linked to in his comment (Different log4j layout for debug and error?). People there seemed to think it was a bad idea but no one ever explained why. It does seem a bit hacky but it does what we need. What are the main reasons to not use that method (other than having to maintain 2 different loggers)?


Here is what I eventually came up with that did the trick:

log4j.rootLogger=INFO,console,stdLog,errorLog

#console appender
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %t %-5p %c{2} - %m%n

#file appender
log4j.appender.stdLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.stdLog.DatePattern = '.'yyyy-MM-dd
log4j.appender.stdLog.File = log/log.txt
log4j.appender.stdLog.layout = org.apache.log4j.PatternLayout
log4j.appender.stdLog.layout.ConversionPattern = [%d{ISO8601}]%5p%6.6r[%t]%x(%F:%L) - %m%n

#file appender
log4j.appender.errorLog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorLog.DatePattern = '.'yyyy-MM-dd
log4j.appender.errorLog.File = log/errorlog.txt
log4j.appender.errorLog.layout = org.apache.log4j.PatternLayout
log4j.appender.errorLog.layout.ConversionPattern = [%d{ISO8601}]%5p%6.6r[%t]%x(%F:%L) - %m%n
log4j.appender.errorLog.Threshold=ERROR

#Specific log levels
log4j.category.com.mypackage1=DEBUG
log4j.category.com.mypackage2=DEBUG

This makes 2 logs, one which logs everything at Warn or above plus at DEBUG or above on the specified packages for the standard log and at ERROR and above only across all packages to the error log.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜