log4j debug messages not showing in console despite isDebugEnabled being true - java

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

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.

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>

Related

How to configure log4j to write DEBUG messages into a file and INFO to console

How can I configure log4j properties file to print DEBUG messages into a file and only INFO messages to console?
This is my properties file:
# Root logger option
log4j.rootLogger=INFO, stdout
log4j.logger.org.hibernate.SQL=TRACE, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{dd-MM-yyyy HH:mm:ss,SS} %t %c{1.}: %m%n
You can use log4j.properties like below:
# Root logger option
log4j.rootLogger=INFO, stdout, file
log4j.logger.org.hibernate.SQL=TRACE, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout..Threshold=INFO
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{dd-MM-yyyy HH:mm:ss,SS} %t %c{1.}: %m%n
# Direct log messages to file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.File=/var/log/logfile.log
log4j.appender.file.Append=true
log4j.appender.file.Encoding=UTF-8
log4j.appender.file.DatePattern='-'yyyy-MM-dd'.log'
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{ISO8601}] %-5p %x%m%n
By this properties file, log4j create new logfile for each day and save previews logfile with date.
Alternative way with log4j xml configuration. Because I prefer to use xml for log4j config. Perhaps this may help you from some way.
eg.
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<appender name="CONSOLE_LOG" 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="%d{ABSOLUTE} %-5p [%c] %m%n" />
</layout>
</appender>
<appender name="FILE_LOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/debug.log" />
<param name="Threshold" value="DEBUG" />
<param name="maxBackupIndex" value="5" />
<param name="maxFileSize" value="1500MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} |%5p| [%c] | %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="CONSOLE_LOG" />
<appender-ref ref="FILE_LOG" />
</root>
</log4j:configuration>
So, your Java code be like:
logger.debug("your debug log message");
logger.info("your info log message");
To configure with log4j.properties, possible duplicate at How can I create 2 separate log files with one log4j config file?
Hope this help!

Writing Only INFO logs to console and ERROR logs to file

I have a requirement to write the INFO (not error and lower levels of INFO) logs to console and ERROR logs to file. Need to use log4j for this.
You need to set a Threshold on your appender for console output. Here's an example modified from the log4j distro:
# A1 is set to be a FileAppender sending its output to
# System.out. However, only info messages and above will be printed
# in A1 because A1's threshold is set to Level.INFO.
# The fact that the root level is set to Prority.DEBUG only influences
# log requests made to the root logger. It has no influence on the
# *appenders* attached to root.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=INFO
This way, anything printed to your ConsoleAppender A1 will be ignored if it's not at least INFO level. If you set the logging level to INFO then ONLY the INFO messages will be logged.
There's an FAQ question about this on the apache log4j site.
Try this.
log.file.path=C:/log
datestamp=yyyy-MM-dd HH:mm:ss.SSS
# Define the root logger with appender file
log4j.rootLogger=INFO, stdout
log4j.logger.biz.netweb=error, LOG_FILE
#, 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{${datestamp}}],[Thread:%t],[%C.%M(%L)]==>: %m%n
# LOG_FILE
log4j.appender.LOG_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOG_FILE.File=${log.file.path}/system-log/system-log.txt
log4j.appender.LOG_FILE.ImmediateFlush=true
log4j.appender.LOG_FILE.MaxFileSize=5MB
log4j.appender.LOG_FILE.MaxBackupIndex=50
log4j.appender.LOG_FILE.Append=true
log4j.appender.LOG_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG_FILE.layout.conversionPattern=%5p,[%d{${datestamp}}],[%t,%C.%M(%L)]==>, %m%n
log4j.rootLogger = debug,stdout,D,E
print to console
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 = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
print to file
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## log file name
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## ERROR log info!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
Following log4j configuration file will logs only "INFO" level logs in to console and between "INFO" and "FATAL" logs in to log.log file.
I you want to change the log level, you can change the MinLevel/MaxLevel parameters of LevelRangeFilter section on both console and Rolling file appender.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--Redirect log messages to console-->
<appender name="stdout" 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="%m"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<!--Redirect log messages to a log file, support file rolling.-->
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO"/>
<param name="File" value="log.log"/>
<param name="maxFileSize" value="2Mb"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<level value="INFO"></level>
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>

log4j : generate log file name with parameters

Am using log4j as follows :
<appender name="LOGFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="D:/direc/logs/back-${batch}-${dateexec}.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd-HH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{dd MMM yyyy - HH:mm:ss,SSS} [%t] %-5p [%c] %M : (ligne %L) - %m %n" />
</layout>
</appender>
where {batch} and {dateexec} should refer to variables[from command parameters ] (batch name and execution time).
in this case the log file name is "back--.log"
Does any one knows how it works ?

Conditional log4j.xml pattern

Because %C and %M are resource intensive, for the pattern
%d{yyyy-MM-dd HH:mm:ss} [%C][%M][%t]
I would like that %C and %M to be effective only if log level is set to debug (or more detailed levels).
Does log4j have any provisions for such conditional output formats?
Similar to what Matt suggested before, you can also attach filters to log Appenders and attach multiple Appenders to a single logger. For example:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%C][%M][%t] - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<appender name="console2" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
</filter>
</appender>
<root>
<priority value="debug" />
<appender-ref ref="console" />
<appender-ref ref="console2" />
</root>
</log4j:configuration>
This should output the patterns you require. The max level filter on the first appender should prevent high log levels and the min level on the second appender prevents debug level statements from appearing twice.
Hope this helps.
You could use another logger that sets additivity="false" for the debug level combined with a ThresholdFilter that only accepts DEBUG or below. It would be a bit of work, though. If that doesn't work, file an issue.

log4j appender priority conflict?

I have this log4j.properties:
# Global logging configuration
log4j.rootLogger=WARN, file
### Direct log messages to a log file
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File= C:\\eclipse\\servers\\apache-tomcat-6.0.39\\logs\\log4j.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Console messages Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Appender-to-Class definition
log4j.logger.com.XXX.payplatform.test.*=INFO, console
log4j.additivity.com.XXX.payplatform.test.*=false
So, ok, the WARN level information is being registered in the log4j.log file. That's OK.
But, I want to see INFO-level messages in my eclipse console, but i am not getting any response there.
Any solutions? I think the problem is in the "WARN, file" rootLogger configuration, so i tried this:
# Global logging configuration
log4j.rootLogger=INFO, file
But no changes resulted...
Thank you!
Read more Apache log4j 1.2 - Short introduction to log4j and find the samples as well.
Level Inheritance
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.
Basic Selection Rule
A log request of level p in a logger with (either assigned or inherited, whichever is appropriate) level q, is enabled if p >= q.
This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.
It should be like this. Add console here and set default root log level to INFO.
log4j.rootLogger=INFO, file, console
OR
Try with log4j.xml instead of log4j.properties. Read more Configuring Log4j 2
Sample log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="FILE_LOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/log4j.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true"/>
<param name="DatePattern" value="'.'dd-MM-yyyy-HH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<category name="com.XXX.payplatform.test">
<priority value="INFO" />
<appender-ref ref="STDOUT"/>
</category>
<root>
<priority value ="ERROR" />
<appender-ref ref="FILE_LOG" />
</root>
</log4j:configuration>

Categories

Resources