Document Version 1.0
Copyright © 2012-2013 beijing.beijing.012@gmail.com
You might have ever seen following warning running your program:
What does this mean?
It means Log4j is not initialized for your application.
Log4j could be initialized by using Log4j configurators. A more convenient way however is to provide a Log4j configuration file as JVM parameter, and let Log4j do the initialization. I will show you how does this work with a small program now:
Sample code:
log4j:WARN No appenders could be found for logger (MyLogger).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN Please initialize the log4j system properly.
What does this mean?
It means Log4j is not initialized for your application.
Log4j could be initialized by using Log4j configurators. A more convenient way however is to provide a Log4j configuration file as JVM parameter, and let Log4j do the initialization. I will show you how does this work with a small program now:
Sample code:
package tryl4j;
import org.apache.log4j.Logger;
/**
* @author swang
*
*/
public class TryLog4jInit {
private static final Logger logger = Logger.getLogger("MyLogger");
public static void main(String[] ages) {
logger.info("Did you add a JVM param for initializing log4j?");
}
}
log4j.properties file
# Root logger option
log4j.rootLogger=INFO, 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.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Run the above class with:
-Dlog4j.configuration="file:/tmp/log4j.properties"
And you will see the warning is not showing up and more, and you see the logged info in console:
2013-06-23 12:13:17 INFO MyLogger:16 - Did you add a JVM param for initializing log4j?
So the JVM parameter works!