简体   繁体   English

如何配置log4j只记录信息消息和更高?

[英]How to configure log4j to log only info messages and higher?

I don't use config or xml files for configuring log4j as there no need to configure it deeply. 我没有使用config或xml文件来配置log4j,因为不需要深入配置它。 I only need to print messages to the console. 我只需要将消息打印到控制台。 so I do this: 所以我这样做:

BasicConfigurator.configure();

and to print only info and higher messages I do this: 并且只打印信息和更高的消息我这样做:

Logger LOG = Logger.getLogger(MyClass.class)
LOG.setLevel(Level.INFO);

But nevertheless I see debug messages in the console too. 但是我在控制台中也看到了调试消息。 But I only need info and actually error messages to be printed. 但我只需要打印信息和实际错误消息。

How to do this? 这该怎么做?

UPD: I created a config file with the which contains, as described in the tutorial here: manual the following: UPD:我创建了一个包含的配置文件,如本教程中所述: 手动以下内容:

  log4j.rootLogger=INFO, stdout

  # Direct log messages to stdout
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.Target=System.out
  log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L        %m%n

log4j.logger.com.messagedna.facade=INFO
log4j.logger.com.messagedna.receiver=INFO
log4j.logger.com.messagedna.util=INFO
log4j.logger.com.messagedna.parser=INFO

I put it to the com.messagedna 我把它放到了com.messagedna

In every class I need logger in I wrote the following: 在我需要记录器的每个班级中,我写了以下内容:

Properties props = new Properties();
try {
    props.load(new FileInputStream("/log4j.properties"));
    } catch (Exception e){
      LOG.error(e);
      }
PropertyConfigurator.configure(props);

but when I run my app I get the following: 但是当我运行我的应用程序时,我得到以下内容:

log4j:WARN No appenders could be found for logger (com.messagedna.server.util.Config).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

You are only setting the level for messages logged using the logger for MyClass.class , however any messages at debug level on other loggers will still get printed. 您只是为使用MyClass.class的记录器记录的消息设置了级别,但是其他记录器上的调试级别的任何消息仍将被打印。

What you really need to do is to set the log level on the console log handler (which is responsible for printing all log messages to the console). 您真正需要做的是在控制台日志处理程序(负责将所有日志消息打印到控制台)上设置日志级别。 However doing this whilst using BasicConfigurator will be quite tricky to do, you are better off moving to having your logging configuration specified in a properties file (which is much more flexible). 但是,在使用BasicConfigurator时执行此操作将非常棘手,最好转到在属性文件中指定日志配置(这样更灵活)。

I would advise working through the log4j logging configuration tutorial - this will help you put together exactly the configuration you want, and should prove a worthwhile investment of your time. 我建议通过log4j日志记录配置教程 - 这将帮助您准确地组合您想要的配置,并且应该证明您的时间是值得的投资。 However, if you want to get this done quickly, try adding the content below to the file log4j.properties ( example taken from here ): 但是,如果您希望快速完成此操作,请尝试将以下内容添加到文件log4j.properties从此处获取示例 ):

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

And then changing your code to do the following: 然后更改代码以执行以下操作:

Properties props = new Properties();
props.load(new FileInputStream("/my/path/to/log4j.properties"));
PropertyConfigurator.configure(props);

Configure log4j.properties: 配置log4j.properties:

log4j.rootLogger=DEBUG, A1

log4j.logger.com.MYPACKAGE=INFO, A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender

Since you want to use to do it only by code 因为你只想用代码来做它

Heres a alternative. 继承人另类。

Do this at the start, a static code initialization will work 在开始时执行此操作,静态代码初始化将起作用

logger = Logger.getLogger(Class.getName(), factory);
logger.setLevel(Level.INFO);

Having said that, theres no excuse for not using log4j.xml. 话虽如此,没有理由不使用log4j.xml。 Its very easy to use one. 它非常容易使用。

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

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