Sunday, June 23, 2013

Initializing Log4j


Document  Version 1.0


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!