java log4j - 如何限制 appender 的包/类的日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17840289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 15:11:16  来源:igfitidea点击:

log4j - How to limit the log of a package/class for an appender

javalogginglog4jhtmlunitappender

提问by Afshin Moazami

I am using the log4j.properties, and I have an appender called RDev.

我正在使用 log4j.properties,并且我有一个名为 RDev 的附加程序。

I want set the level of RDev appender to DEBUG except for specific packages (HTMLUnit in my case)

我想将 RDev appender 的级别设置为 DEBUG,除了特定的包(在我的例子中是 HTMLUnit)

My current log4j.properties is like this:

我目前的 log4j.properties 是这样的:

log4j.rootLogger=debug RDev, test

log4j.appender.RDev=org.apache.log4j.RollingFileAppender
log4j.appender.RDev.File=D:/logs

log4j.appender.RDev.MaxFileSize=5mb
log4j.appender.RDev.MaxBackupIndex=10

log4j.appender.RDev.layout=org.apache.log4j.PatternLayout
log4j.appender.RDev.layout.ConversionPattern=[%5p %d{MMM/dd HH:mm:ss}] [%t] (%F:%L) - %m%n

and I want to filter/remove the DEBUG log of these packages:

我想过滤/删除这些包的调试日志:

com.gargoylesoftware.htmlunit
org.apache.commons.httpclient
org.apache.http.impl.conn

*UPDATE *I tried this and it works for all appendars:

*更新 *我试过这个,它适用于所有的附录:

# Set specific logger levels.
log4j.logger.org.mortbay.log=fatal
log4j.logger.org.apache.http=fatal
log4j.logger.org.apache.http.headers=fatal
log4j.logger.org.apache.http.wire=fatal

(from https://code.google.com/p/spidey/wiki/HtmlUnitErrors)

(来自https://code.google.com/p/spidey/wiki/HtmlUnitErrors

Now the question is how to limit this configs to one appendar? (not all of them) (e.g. I want to see the debugs in the file, not console.)

现在的问题是如何将此配置限制为一个附件?(不是全部)(例如,我想查看文件中的调试,而不是控制台。)

采纳答案by Tim

Do you still want to see log lines for levels that are above DEBUG for those packages? If so, this log4j tutorialfrom Apache describes one way to do that; look a bit more than halfway down, at the text that reads "Suppose we are no longer interested in seeing the output of any component belonging to the com.foo package."

您是否仍想查看那些包的高于 DEBUG 级别的日志行?如果是这样,Apache 的这个 log4j 教程描述了一种方法;再往下看,上面写着“假设我们不再对查看属于 com.foo 包的任何组件的输出感兴趣。”