1

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?

4
  • 3
    What do the Log4j docs say? Commented Aug 23, 2016 at 14:16
  • I agree with bradimus. Why are asking other people what is nicely documented, probably a zillion times? See for example: stackoverflow.com/questions/5817738/… Commented Aug 23, 2016 at 14:20
  • If rootLogger is set to DEBUG it will print out DEBUG, INFO, WARN, ERROR. If you want to filter for INFO and ERROR, only then you need to configure a filter in your appenders. See stackoverflow.com/documentation/java/2472/log4j-log4j2/19786/… Commented Aug 23, 2016 at 14:27
  • 1
    ok thanks, I am also going through Log4j docs as per previous comments Commented Aug 23, 2016 at 14:31

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.