log4j:WARN No appenders could be found for logger X

Nothing major, no revelation. Just a thing I hit rarely but at the same time often enough that I don’t want to search again what it exactly means. Thus – this entry.

Quick version: use debug flag (-Dlog4j.debug) to see if file was picked up. Then check if appenders are set for mentioned log levels – you have specific logger being given as problematic, check it first. If that’s not enough…

Not in your classpath

log4j library uses log4j.xml first. If none found, it uses log4j.properties. Library searches the classpath and pointed locations (if any).

To check if the file was found, add -Dlog4j.debug to the command line. Exemplary output with found .properties file:


log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@53372a1a.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@53372a1a class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@53372a1a.
log4j: Using URL [file:/home/tammo/projects/DbUnit-Example/target/classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/home/tammo/projects/DbUnit-Example/target/classes/log4j.properties
log4j: Parsing for [root] with value=[ERROR, stdout].
log4j: Level token is [ERROR].
log4j: Category root set to ERROR
log4j: Parsing appender named "stdout".
log4j: Parsing layout options for "stdout".
log4j: Setting property [conversionPattern] to [%d %5p %t [%c] - %m%n].
log4j: End of parsing for "stdout".
log4j: Parsed "stdout" options.
log4j: Parsing for [org.springframework] with value=[ERROR].
log4j: Level token is [ERROR].
log4j: Category org.springframework set to ERROR
log4j: Handling log4j.additivity.org.springframework=[null]
log4j: Parsing for [org.hibernate] with value=[ERROR].
log4j: Level token is [ERROR].
log4j: Category org.hibernate set to ERROR
log4j: Handling log4j.additivity.org.hibernate=[null]
log4j: Finished configuring.

Check classpath

If you don't have a file listed, it was not picked up. Check class path - checking class path depends on your OS, but it's simple anyway:

echo $CLASSPATH # GNU/Linux
echo %CLASSPATH% # Windows

Your IDE most likely gives you options to see and modify Java classpath - see project settings, build path etc.

No appender for the file

No universal rule here, but browse your file carefully to see if you have ANY appender that can service the logger X that log4j complains about.
In my recent case Spring logger was set to [ALL] log level, but my only appender was for ERROR and thus receipts were two:

  • - reset Spring logger to error level (as I did, why bother with TRACE or DEBUG if all works).
  • - set aside special appender for Spring, making it a FILE appender, since it would take in lots of data (ALL levels, remember?).
  • File not read

    Location of the file should be given as an URL, and URL require some information about protocols BEFORE the actual location.
    http://location.of.your.website
    file://location/of/your/file -> with Windows it can look peculiar, with drives like C:, D: etc.

    Admittedly, I've done only basic research here since I wasn't hit by this even once (Linux rules?).

    Sources

    Apache log4j default initialization procedure manual

    Apache log4j documentation and tutorial by TutorialsPoint - examples, rules, all. Detailed.
    Example log file with comments explaining which line does what. Suffices (quite so!) for basic cases.

    Checking classpath

    Jaikiran's tech blog entry dedicated to same problem with several user comments detailing their problems and solutions.

    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s