How to configure log4j to log only info messages a

2019-05-02 10:13发布

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.

标签: java log4j
3条回答
劳资没心,怎么记你
2楼-- · 2019-05-02 10:56

Configure log4j.properties:

log4j.rootLogger=DEBUG, A1

log4j.logger.com.MYPACKAGE=INFO, A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender
查看更多
▲ chillily
3楼-- · 2019-05-02 11:06

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);
查看更多
萌系小妹纸
4楼-- · 2019-05-02 11:09

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. Its very easy to use one.

查看更多
登录 后发表回答