15

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
peterp
  • 3,101
  • 3
  • 22
  • 37
  • 2
    can you add log4j.properties ? maybe you're missing an appender... – BigMike Feb 26 '13 at 10:35
  • 1
    Sorry, thought we would not need this, since `isDebugEnabled` returned `true` already, but of course, you are right. I have updated the question. – peterp Feb 26 '13 at 10:41
  • isDebugEnabled() is not enough, you may have filters (as you indeed have). See StephenC comment on baraky answer. – BigMike Feb 26 '13 at 10:47

2 Answers2

11
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.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
  • 2
    +1 - Debug logging is enabled (from the root logger down) ... but both of his appenders are filtering out log events below "INFO". – Stephen C Feb 26 '13 at 10:44
  • Ouch... of course! One should think that naming a config parameter `threshold` should be obvious... I just did not notice it :( Thanks a lot! After an additional read of [this SO answer](http://stackoverflow.com/questions/5119883/log4j-what-is-threshold) I now actually understand what exactly is going on here. – peterp Feb 26 '13 at 10:50
1

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>
Ranu Jain
  • 577
  • 4
  • 11