如何配置log4j只记录信息消息和更高? [英] How to configure log4j to log only info messages and higher?

查看:90
本文介绍了如何配置log4j只记录信息消息和更高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不使用config或xml文件来配置log4j,因为不需要深入配置它。我只需要将消息打印到控制台。所以我这样做:

  BasicConfigurator.configure(); 

并且只打印信息和更高的消息我这样做:

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

但是我也在控制台中看到调试消息。但我只需要打印信息和实际错误消息。



如何做到这一点?



UPD:我创建了一个包含的配置文件,如教程中所述:
手册以下内容:

  log4j.rootLogger = INFO,stdout 

#将日志消息直接发送到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

我把它放了到 com.messagedna



在ev我需要记录器的ery类我写了以下内容:

 属性props = new Properties(); 
try {
props.load(new FileInputStream(/ log4j.properties));
} catch(例外e){
LOG.error(e);
}
PropertyConfigurator.configure(props);

但是当我运行我的应用程序时,我得到以下内容:

  log4j:WARN找不到logger(com.messagedna.server.util.Config)的appender。 
log4j:WARN请正确初始化log4j系统。
log4j:警告有关详细信息,请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig。


解决方案

您只需设置使用记录器为 MyClass.class ,但是其他记录器上的调试级别的任何消息仍会被打印。



你是什么真正需要做的是在控制台日志处理程序(它负责将所有日志消息打印到控制台)上设置日志级别。但是,使用 BasicConfigurator 执行此操作将非常棘手,最好转到在属性文件中指定日志配置(这样更灵活)。 / p>

我建议通过 log4j进行工作日志配置教程 - 这将帮助您准确地组合您想要的配置,并且应该证明您的时间值得投入。但是,如果您希望快速完成此操作,请尝试将以下内容添加到文件 log4j.properties 从这里取的示例):

  #Root logger选项
log4j.rootLogger = INFO,stdout

#将日志消息直接发送到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

然后更改代码以执行以下操作:

 属性props = new Properties(); 
props.load(new FileInputStream(/ my / path / to / log4j.properties));
PropertyConfigurator.configure(道具);


I don't use config or xml files for configuring log4j as there no need to configure it deeply. 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:

  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

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.

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

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. However, if you want to get this done quickly, try adding the content below to the file log4j.properties (example taken from here):

# 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);

这篇关于如何配置log4j只记录信息消息和更高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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