Tuesday, 4 February 2014

Setup log4j with Struts 2 in eclipse

Apache log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is now a project of the Apache Software Foundation. log4j is one of several Java logging frameworks.

Steps for setting up log4j with Struts 2 are given below:

1. Download the log4j jar file from here .

2. Add log4j.jar into your project build path.

3. Add log4j.jar into your WEB-INF/lib folder.


4.Add log4j.properties into your classpath (e.i. where you put struts.xml).

and put below line of code in log4j.properties

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\logFile.txt
log4j.appender.file.MaxFileSize=10MB
# Set the the backup index
log4j.appender.file.MaxBackupIndex=10
# Set the append to false, should not overwrite
log4j.appender.file.Append=false
# Set the threshold to debug mode
log4j.appender.file.Threshold=debug
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# 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{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=DEBUG, file, stdout

5.Now you can use your Logger in your Action class

private static final Logger logger=Logger.getLogger(ClassName.class.getName());

log4j.appender.file.MaxFileSize=10MB  ggiviis used to create the logfile of 10MB after that another file will be automatically create if threshold exceeds , How many files can be created is configured via 
log4j.appender.file.MaxBackupIndex=10  so here total size is 10.

No comments:

Post a Comment