Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j debug messages not showing in console despite isDebugEnabled being true

I am using the following code within my project to log debug messages with log4j

private static final Logger LOG = Logger.getLogger(MyClass.class)
// ...
if(LOG.isDebugEnabled()) {
    LOG.debug("my log message");
}

I can confirm that my log4j configuration is correct by adding a break point at the line where the debug message is written, i.e. LOG.isDebugEnabled() does return true. Interestingly, my debug message does not show up in the console of my IDE (IntelliJ), however when changing LOG.debug() to LOG.info(), the info message is logged as expected.

Any ideas what I should be looking for in order to find out what's going wrong here?

EDIT: here's my log4j.properties file

log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%-5p [%d{dd.MM.yy HH:mm:ss}] %C{1} - %m [thread: %t]\n
log4j.appender.Stdout.threshold=info

log4j.appender.StandaloneFile=org.apache.log4j.RollingFileAppender
log4j.appender.StandaloneFile.File=logs/standalone.log
log4j.appender.StandaloneFile.MaxFileSize=5MB
log4j.appender.StandaloneFile.MaxBackupIndex=20
log4j.appender.StandaloneFile.layout=org.apache.log4j.PatternLayout
log4j.appender.StandaloneFile.layout.ConversionPattern=%-5p [%d{dd.MM.yy HH:mm:ss}] %C{1} - %m [thread: %t]\n
log4j.appender.StandaloneFile.threshold=info

log4j.rootLogger=info, Stdout, StandaloneFile
log4j.logger.com.myPacke.package1=info, Stdout, StandaloneFile

log4j.logger.com.myPacke.package2=DEBUG
like image 405
peterp Avatar asked Feb 26 '13 10:02

peterp


People also ask

How do I enable debug mode in log4j properties?

You need to set the logger level to the lowest you want to display. For example, if you want to display DEBUG messages, you need to set the logger level to DEBUG. The Apache log4j manual has a section on Configuration.

Which log4j component is responsible for logging messages?

log4j has three main components: loggers: Responsible for capturing logging information. appenders: Responsible for publishing logging information to various preferred destinations. layouts: Responsible for formatting logging information in different styles.

What is difference between logger INFO and logger debug?

INFO is used to log the information your program is working as expected. DEBUG is used to find the reason in case your program is not working as expected or an exception has occurred. it's in the interest of the developer.

Is logger isDebugEnabled necessary?

It's useful when the String your passing to logger. debug(...) takes time to evaluate, in that case you can skip this evaluation if debug is not enabled. IMO this makes the code a lot less readable so it should only be used when there's a significant performance improvement.


2 Answers

log4j.appender.Stdout.threshold=info

Should be:

log4j.appender.Stdout.threshold=debug

You just set the console threshold to be info, so you're not getting debug level logs.

Be aware you also set the RollingFileAppender threshold to info as @Stephen C commented.

like image 189
BobTheBuilder Avatar answered Sep 20 '22 16:09

BobTheBuilder


Make sure your configuration has below appender...We have used log4j.xml so i am adding from xml

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <param name="Threshold" value="info" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p: %c - %m%n" />
    </layout>
</appender>

<appender name="logfile" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="log/dcm_migration.log" />
    <param name="Append" value="false" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d  %-5p  [%c{1}] %m %n" />
    </layout>
</appender>
like image 27
Ranu Jain Avatar answered Sep 18 '22 16:09

Ranu Jain