Friday, August 1, 2008

Simplest way to Configure Logging using Log4j

Ensure the log4j.jar & log4j.properties are in the classpath

Add the following into the required classes where logging is needed , so that log shows file name for each logger statement .

private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger([classname].class);

folowed by logger.[info,debug,error,fatal etc]("logger message");

Contents in Log 4j.properties

log4j.rootLogger=ON, ROOT[enabling the root logger]
log4j.appender.ROOT=org.apache.log4j.DailyRollingFileAppender[saving logs on the daily basis with date appended]
log4j.appender.ROOT.File=/a/b/c.log[root log file path]
log4j.appender.ROOT.layout=org.apache.log4j.PatternLayout
log4j.appender.ROOT.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss } %-5p [%t] %c{2} - %m%n [pattern for display in each line]

#FILE APPENDER [another logger file ]
log4j.appender.F2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.F2.File=/a/b/d.log
log4j.appender.F2.layout=org.apache.log4j.PatternLayout
log4j.appender.F2.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss } %-5p [%t] %c{2} - %m%n


log4j.logger.org.apache=ERROR[apache specific errors to be shown on both logger files]
log4j.logger.com.example=DEBUG,F2[for com.example modules debug status messages to be shown in second logger file]

For Log4j debugging
Append -Dlog4j.debug in JAVA_OPTS in catalina.sh which will enable detailed logs in catalina.out showing the logger configerations.

No comments:

 
Free Domain Names @ .co.nr!