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);
Configure 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. Its very easy to use one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With