Friday, October 8, 2010

log4j configuration:: file and console logging

Very often i am required to do the log4j configuration and some how it doesnt work; so i thought i will have a working sample copy with me so that i can extend from this whenever required; I guess this helps somebody as well!
#logging both to R and C appenders
log4j.rootCategory=DEBUG, R, C


#FILE APPENDER:R
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%c - %d{yyyy-MM-dd hh:mm:ss} %-5p [%t] %x (%F) - %m%n
log4j.appender.R.File=D:\\default.log
log4j.appender.R.Append=true
log4j.appender.R.Threshold=DEBUG
log4j.appender.R.DatePattern=.yyyy-MM-dd


#console appender:C
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%c - %d{yyyy-MM-dd hh:mm:ss} %-5p [%t] %x (%F) - %m%n
log4j.appender.C.Append=true
log4j.appender.C.Threshold=DEBUG

#log starting with name abc are directed to R appender i.e.. file
log4j.logger.abc=DEBUG,R
log4j.additivity.abc=false


  • Say you want to figure out which log file is being pickedup then give the below at the java startup
    • java  -Dlog4j.debug=true abc.java
  • Say there are many log4j.properties around and you want only one of them to be pickedup then
    • java  -Dlog4j.debug=true abc.java -Dlog4j.configuration=file:///path/to/log4j/log4j.properties

No comments:

Post a Comment