Log4j

*Download log4j (apache-log4j-1.2.15.zip) from its download site
*Unzip
*Place log4j-1.2.8.jar in classpath
*Create a log4j.properties in classpath

log4j.rootLogger=debug, stdout, R
 
# Specify which package to log
log4j.logger.com.my.pkg=INFO, R
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
 
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
 
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

*Usage

import org.apache.log4j.*;
public class TestClass extends TestCase {
	Logger log = Logger.getLogger(TestClass .class.getName());
	public void testOne() {
        	log.debug("Testing...");
        }

* Specify log4j.properties. Defaults to log4j.properties

-Dlog4j.configuration=mylog4j.properties
This entry was posted in java. Bookmark the permalink.