Surender Raja
Surender Raja

Reputation: 3599

Log4j Logging Level in java

Let's say I have the below line in log4j.properties

log4j.rootLogger=DEBUG,file
..
..

Does my java application capture all loggings with log.debug() alone ?

or

Does my java application capture all loggings with debug,info, warn,and error also

What do I need to do if I need only INFO and ERROR?

Upvotes: 1

Views: 2127

Answers (1)

Arthur Noseda
Arthur Noseda

Reputation: 2654

The best course to really understand is to see for yourself.

LogLevelDemo.java

public class LogLevelDemo {

    private static final Logger LOG = Logger.getLogger(LogLevelDemo.class);

    public void log() {
        LOG.fatal("This is fatal.");
        LOG.error("This is error.");
        LOG.warn("This is warn.");
        LOG.info("This is info.");
        LOG.debug("This is debug.");
        LOG.trace("This is trace.");
    }

    public static void main(String[] args) {
        LogLevelDemo lld = new LogLevelDemo();
        lld.log();
    }

}

log4j.properties

log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender

log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

And then switch the log4j.rootLogger level attribute to the level your want to test.

these settings print the following:

0    [main] FATAL demo.log4j.LogLevelDemo  - This is fatal.
0    [main] ERROR demo.log4j.LogLevelDemo  - This is error.
0    [main] WARN  demo.log4j.LogLevelDemo  - This is warn.
0    [main] INFO  demo.log4j.LogLevelDemo  - This is info.

Hence no debug or trace statements, and so on and so forth.

Upvotes: 2

Related Questions