www.javadevhome.com

Google
   

log4j

   
 
static Logger logger = Logger.getRootLogger();

BasicConfigurator.configure();
logger.setLevel(Level.ALL);
String pp = "%d %t %m%n";
PatternLayout layout = new PatternLayout(pp);
ConsoleAppender appender = new ConsoleAppender(layout);
logger.removeAllAppenders();
logger.addAppender(appender);



// With log4j.properties file in the code:

PropertyConfigurator.configure(ProgConstants.LOG4J_CONFIG_FILE);



// In the log4j.properties file:

log4j.rootLogger=debug, stdout, 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 %t %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=%p %t %c - %m%n



// For Tomcat
// In the start script add:
# unix : to place the log4j.properties file under the WEB-INF/classes
export TOMCAT_OPTS="-Dlog4j.configuration=log4j.properties"

# Windows : to place the log4j.properties file under the c:/log4j.properties
set TOMCAT_OPTS=-Dlog4j.configuration=file:/c:/log4j.properties