12
votes

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.

3

3 Answers

11
votes

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.

1
votes

You need to add the protocol to the front of System property value like so: -Dlog4j.configuration=file://log4j-test.properties

Without the protocol it will look in the classpath.

0
votes

jul - commons-logging, it likes your situation.

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

set log4j.properties, before instance of Log.

System.setProperty("log4j.configuration", "log4j.properties");