在 log4j 中为记录器设置不同的日志记录级别
我在我的应用程序中使用 log4j - 我需要查看所有日志消息,所以我定义了
# General configuration
log4j.rootLogger = ALL, ConsoleAppender
# Appender configuration
log4j.appender.ConsoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern = %5p (%c) %m%n
但是,在我开始使用 Apache 的 HTTPClient 库(顺便说一句,这很棒)之后,我的控制台因其日志记录而变得模糊 - 所有内容都被记录下来,我我无法再看到我自己的应用程序的日志输出。
那么,我如何告诉 HTTPClient 库它应该只记录 WARN
消息,而我自己的应用程序仍然记录 ALL
级别?
我使用创建我的应用程序记录器
Logger logger = Logger.getLogger(MyClass.class);
PropertyConfigurator.configure("log4j.properties");
I am using log4j for my application - there I need to see all log messages, so I defined
# General configuration
log4j.rootLogger = ALL, ConsoleAppender
# Appender configuration
log4j.appender.ConsoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern = %5p (%c) %m%n
But, after I started using Apache's HTTPClient library (which is awesome btw), my console got clouded up by its logging - everything was logged and I am not able to see the log output from my own application any more.
So how can I tell the HTTPClient library that it should only log WARN
messages, while my own application still logs on ALL
levels?
I create my application logger using
Logger logger = Logger.getLogger(MyClass.class);
PropertyConfigurator.configure("log4j.properties");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很简单。只需添加以下行即可。
同样,您可以通过将任何包
com.foo
或类com.foo.Bar
附加到前缀log4j.logger
来配置级别,如下所示如下:It's quite simple. Just add the below line.
Similarly, you can configure levels for any package
com.foo
or classcom.foo.Bar
by appending it to the prefixlog4j.logger
as follows: