Log4j 1.2 is deprecated version of Log4J but it is still used extensively. It is default implementation apache commons-logging. I am going to explain configuration of log4j 1.2 in this post.
My aim it to log all message with log level ERROR and higher to log file and console both and messages with level TRACE and higher to log file. This way console will receive higher priority messages and more detailed logs will be logged to the log file.
To achieve this I create two appenders A and R. A is console appender. I will set threshold of appender A to ERROR so any message lower than ERROR will not be logged with this appender. Appender R logs to tomcat.log and it does not have any threshold attached to it.
The location for log4.properties need to be specified using system property log4j.configuration. For example -Dlog4j.configuration=/my/path/log4j.properties
Please note that log4j.configurationFile system property is used by Log4J 2 which has a different syntax.
log4j.rootLogger=ERROR,A,R
#*** A is the console appender
log4j.appender.A=org.apache.log4j.ConsoleAppender
#*** A uses pattern layout
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.threshold=ERROR
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p {%F:%L} %x - %m%n
#**** R is the Rolling FileAppender
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=/home/tomcat/logs/tomcat.log
log4j.appender.R.DatePattern='.'yyyy-MM-dd
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p {%F:%L} %x - %m%n
#*** Log Levels
log4j.logger.org.apache=TRACE
No comments:
Post a Comment