Wednesday, October 31, 2012

Log4j create separate log files

In apache tomcat, by default create log files of every event performed in server
and lot web-project deploy on server and all log merge in one log
So how create separate log file for each project


the apache provide log4j library for logging in java by using  this library we easily track event by log object


need to download latest log4j jar file from this link http://logging.apache.org/
add this jar file in classpath
and create a log4j.properties file where we mention about


#Set root category priority to All(info,debug, error, info etc) and its only appender to File
log4j.rootCategory=DEBUG, Cust_Error

#daily rolling log file
log4j.appender.Cust_Error=org.apache.log4j.DailyRollingFileAppender
#create log file apache log folder
log4j.appender.Cust_Error.File=${catalina.home}/logs/SaralHiring.log
#date format ,appends for previous dated log file
log4j.appender.Cust_Error.DatePattern='.'yyyy-MM-dd
log4j.appender.Cust_Error.Append=true
log4j.appender.Cust_Error.Encoding=UTF-8
#if only track error
log4j.appender.Cust_Error.Threshold=ERROR
#for particular pattern message in log
log4j.appender.Cust_Error.layout=org.apache.log4j.PatternLayout
log4j.appender.Cust_Error.layout.ConversionPattern=%d [%-5p] [%t]: [%C{1}] %m%n


in java program

first declare the log object like this

private static final org.apache.commons.logging.Log  log = org.apache.commons.logging.LogFactory.getLog(Home.class);

than call below methods
log.trace("");
log.debug("");
log.info("");
log.warn("");
log.error("");
log.fatal("");

No comments:

Post a Comment