Monday, September 30, 2013

Log4J: Simple File configuration example


## HelloWorld class
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class HelloWorld {

static final Logger logger = Logger.getLogger(HelloWorld.class);

public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}

## log4j.properties file
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.FileAppender ## file apenfder configuration
log4j.appender.CA.File=main.log ## log file name
log4j.appender.CA.layout=org.apache.log4j.PatternLayout

log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 

Referecnes:
http://www.dzone.com/tutorials/java/log4j/log4j-configuration.html
http://www.dzone.com/tutorials/java/log4j/log4j-file-appender-example-1.html

No comments: