jakub holý

building the right thing, building it right, fast

Troubleshooting logging configuration (Log4j, commons-logging)

2009-12-07Languages

Did it ever happen to you that your logging didn't behave as expected? Here are some tips how to find out what's going on.

Commons-logging (since 1.1)

Set the system property org.apache.commons.logging.diagnostics.dest to STDOUT (or STDERR or a file name); docs: -Dorg.apache.commons.logging.diagnostics.dest=STDOUT

Extract of a sample output (no commons-logging.properties):

...
[LogFactory from sun.misc.Launcher$AppClassLoader@934873913] [ENV] Application classpath (java.class.path): ..
...
[LOOKUP] No properties file of name 'commons-logging.properties' found
....
Discovering a Log implementation...
...
Log adapter 'org.apache.commons.logging.impl.Log4JLogger' from classloader java.net.URLClassLoader@32689826 has been selected for use.


Extract of a sample output (incorrect commons-logging.properties found):

...
[LogFactory from sun.misc.Launcher$AppClassLoader@934873913] [LOOKUP] Properties file found at 'jar:file:/myproject/lib/test/dbunit-embeddedderby-parenttest-sources.jar!/commons-logging.properties' with priority 0.0
.. [LOOKUP] Properties file at 'file:/myproject/web/WEB-INF/classes/commons-logging.properties' with priority 0.0 does not override file at 'jar:file:/myproject/lib/test/dbunit-embeddedderby-parenttest-sources.jar!/commons-logging.properties' with priority 0.0
.. [LOOKUP] Properties file of name 'commons-logging.properties' found at 'jar:file:/myproject/lib/test/dbunit-embeddedderby-parenttest-sources.jar!/commons-logging.properties"
.. Attempting to load user-specified log class 'org.apache.commons.logging.impl.SimpleLog'...
.. Log adapter 'org.apache.commons.logging.impl.SimpleLog' from classloader sun.misc.Launcher$AppClassLoader@934873913 has been selected for use.
...


Notice that Commons Logging uses the context classloader and not e.g. Class.getClassloader() to locate the implementation to use, which may occassionally lead to some problems.

PS: You may be better of not using commons-logging because of its classloader issues. (SLF4J may be better?)

Log4j

Set the system property log4j.debug to true for Log4j to log the location of the configuration file it's using and other useful information. You can also set it in in the log4j.properties file:
log4j.debug=true
Or, as mentioned above, pass it as a system property to Java, for example as in
java -Dlog4j.debug=true -jar YourApplication.jar
The debug information will be printed into the System.out, not in any log file you may have configured (Log4j can't use itself for this logging).

Example output:


log4j: Parsing for [root] with value=[INFO, CONSOLE, filelog].
log4j: Level token is [INFO].
log4j: Category root set to INFO
log4j: Parsing appender named "CONSOLE".
log4j: Parsing layout options for "CONSOLE".
log4j: Setting property [conversionPattern] to [%6rms [%p] ..%0.46c %x- %m%n].
log4j: End of parsing for "CONSOLE".
log4j: Parsed "CONSOLE" options.
log4j: Parsing appender named "filelog".
log4j: Parsing layout options for "filelog".
log4j: Setting property [conversionPattern] to [%6rms [%p] ..%0.46c %x- %m%n].
log4j: End of parsing for "filelog".
log4j: Setting property [file] to [/home/me/mylog.log].
log4j: Setting property [maxBackupIndex] to [5].
log4j: Setting property [maxFileSize] to [50MB].
log4j: setFile called: /home/me/mylog.log, true
log4j: setFile ended
log4j: Parsed "filelog" options.
log4j: Parsing for [eu.ibacz.lqs.uiradrupdater] with value=[DEBUG].
log4j: Level token is [DEBUG].
log4j: Category eu.ibacz.lqs.uiradrupdater set to DEBUG
log4j: Handling log4j.additivity.eu.ibacz.lqs.uiradrupdater=[null]
log4j: Finished configuring.


For the log4j.properties:


log4j.rootCategory=INFO, CONSOLE, filelog

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%6rms [%p] ..%0.46c %x- %m%n

log4j.appender.filelog=org.apache.log4j.RollingFileAppender log4j.appender.filelog.File=${user.home}/mylog.log log4j.appender.filelog.MaxFileSize=50MB log4j.appender.filelog.MaxBackupIndex=5 log4j.appender.filelog.layout=org.apache.log4j.PatternLayout log4j.appender.filelog.layout.ConversionPattern=%6rms [%p] ..%0.46c %x- %m%n

log4j.logger.eu.ibacz.lqs.uiradrupdater=DEBUG