views:

888

answers:

1

I am trying to use log4j via commons-logging and having problems if the log4j properties file is not called log4.properties. I get following error: log4j:WARN No appenders could be found for logger (LogMePlease). log4j:WARN Please initialize the log4j system properly.

My code is very simple:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogMePlease 
{
static Log l = LogFactory.getLog(LogMePlease.class);

public static void main(String [] args)
{
 l.warn("Hello World!");
}
}

In my class path, i have: commons-logging.properties file which contains following entries

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.configuration=log4j-test.properties

and log4j-test.properties file

when i run this code i get

log4j:WARN No appenders could be found for logger (LogMePlease).
log4j:WARN Please initialize the log4j system properly.

If I rename log4j-test.properties file to be log4j.properties - then everything works. So, the question is how can I setup commons logging to use arbitrary name for log4j.properties file.

+2  A: 

The file commons-logging.properties is only read from commons logging while log4j will look for log4j.configuration in the system properties.

So you must either specify them with -Dlog4j.configuration=log4j-test.properties on the command line as a JVM option or you must call System.setProperty() before the first call to any logging method (which is usually pretty hard to achieve).

Note: If you can, use the XML config log4j.xml; it's much more simple and powerful for configuring log4j.

Aaron Digulla