javalog4j

How to enable Logger.debug() in Log4j


While trying to execute the following lines only the last two statements are displayed("Here is some ERROR" and "Here is some FATAL") and the first three statements are not displayed.I had just started to learn this topic, can anyone tell why is this happening?

    logger.debug("Here is some DEBUG");
    logger.info("Here is some INFO");
    logger.warn("Here is some WARN");
    logger.error("Here is some ERROR");
    logger.fatal("Here is some FATAL");

the log4j.property has

log4j.rootLogger=debug,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=[%5p] %d{mm:ss}(%F:%M:%L)%n%m%n%n

Solution

  • You probably have a log4j.properties file somewhere in the project. In that file you can configure which level of debug output you want. See this example:

    log4j.rootLogger=info, console
    
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
    
    log4j.logger.com.example=debug
    

    The first line sets the log level for the root logger to "info", i.e. only info, warn, error and fatal will be printed to the console (which is the appender defined a little below that).

    The last line sets the logger for com.example.* (if you get your loggers via LogFactory.getLogger(getClass())) will be at debug level, i.e. debug will also be printed.