开发者

Log4j multiple files

I have 2 开发者_如何学Clogging files. I have defined two appenders for the 2 files. In a class, if i need to output few logs to one file, and few to the other, do i need to have 2 logger instances. Is there a cleaner way of achieving this requirement? Or is there some log4j configuration that will help me?


You can create two named loggers like the following:

log4j.logger.system=debug, sys

log4j.appender.sys=org.apache.log4j.RollingFileAppender
log4j.appender.sys.file=/logs/system.log
log4j.appender.sys.maxFileSize=1MB
log4j.appender.sys.maxBackupIndex=25
log4j.appender.sys.layout=org.apache.log4j.PatternLayout
log4j.appender.sys.layout.conversionPattern=%d{MMM dd HH:mm:ss} %-5p (%F:%L) - %m%n

which you can locate and use from code:

Logger.getLogger("system").debug("...");


If this log filtering is based on severity, you can define a different log level for each appender (DEBUG, INFO, WARNING, etc.).

If you want a different filtering, you can create a personnal log filter, and apply this filter on your appender. Look at http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/spi/Filter.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜