使用类别时在log4j中配置多个日志文件 [英] Configuring multiple log files in log4j while using categories

查看:97
本文介绍了使用类别时在log4j中配置多个日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要执行的操作:我需要2个日志文件:第一个记录INFO级别,并且记录了应用程序所有部分的信息,但还记录了DEBUG和某些包的信息.第二个仅记录ERROR并记录所有软件包的记录.我敢肯定这可能是微不足道的,但我不太清楚.这是我当前正在使用的配置文件:

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.txt
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

显然,这没有错误日志部分.我的基本想法是添加另一个追加器并将其日志级别设置为ERROR,但是类别似乎覆盖了它以及它们的信息,这不是我想要的.之所以存在它们,是因为其他软件包会在设置为调试时转储大量不需要的信息,这就是我们如何解决这些问题的方法.我认为可能有更好的整体方法,但这是我的第一个log4j配置文件.

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.

更新:一位同事建议使用2个记录器,如kdgregory在其评论中链接到的帖子中所述(

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

这将生成2个日志,其中一个日志将所有警告或以上记录以及DEBUG或以上的所有记录记录在标准日志的指定包上,而ERROR或以上记录的所有包都记录在错误日志中.

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.

这篇关于使用类别时在log4j中配置多个日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆