简体   繁体   English

在log4j中为记录器设置不同的日志记录级别

[英]Set differnet levels of logging for loggers in log4j

I am using log4j for my application - there I need to see all log messages, so I defined 我正在使用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

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. 但是,在我开始使用Apache的HTTPClient库(这是非常棒的btw)后,我的控制台因其日志记录而变得模糊不清 - 所有内容都被记录下来,我无法再看到自己应用程序的日志输出了。

So how can I tell the HTTPClient library that it should only log WARN messages, while my own application still logs on ALL levels? 那么我如何告诉HTTPClient库它应该只记录WARN消息,而我自己的应用程序仍然登录ALL级别?

I create my application logger using 我使用创建我的应用程序记录器

Logger logger = Logger.getLogger(MyClass.class);
PropertyConfigurator.configure("log4j.properties");

It's quite simple. 这很简单。 Just add the below line. 只需添加以下行。

log4j.logger.org.apache.commons.httpclient=WARN

Similarly, you can configure levels for any package com.foo or class com.foo.Bar by appending it to the prefix log4j.logger as follows: 类似地,您可以通过将其附加到前缀log4j.logger来为任何包com.foo或类com.foo.Bar配置级别,如下所示:

log4j.logger.com.foo=MYLEVEL
log4j.logger.com.foo.Bar=DEBUG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM