Syntax highlighter header

Showing posts with label Log4j. Show all posts
Showing posts with label Log4j. Show all posts

Saturday, 5 September 2020

Configuring Log4j 2

In this post I am going to explain configuring Log4J 2 to log high severity log messages to console and file both and lower severity messages only to log file. This way console is not cluttered with unnecessary messages and detail information is available for debugging in log file.

I will define two appenders A and R. The appender A has a threshold filter applied to it which accepts only log messages with severity level ERROR and higher. The appender R don't have any filter attached to it, so it will log all messages.

The location of log4j2.xml need to be provided using system property log4j.configurationFile, for example -Dlog4j.configurationFile=/my/path/log4j2.xml

 Please note that system property log4j.configuration is used for Log4J 1.2 which has different syntax.



<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
        <Appenders>
                <Console name="A" target="SYSTEM_OUT">
                        <PatternLayout pattern="%d [%t] %-5p {%F:%L} %x - %m%n" />
                        <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
                </Console>

                <RollingFile name="R"
                        fileName="/home/tomcat/logs/tomcat-log4j2.log" filePattern="/home/tomcat/logs/tomcat-log4j2-%d{yyyy-MM-dd}.log">
                        <PatternLayout pattern="%d [%t] %-5p {%F:%L} %x - %m%n" />
                        <Policies>
                                <TimeBasedTriggeringPolicy interval="1" />
                        </Policies>
                </RollingFile>
        </Appenders>


        <Loggers>
                <Logger name="org.apache" level="trace"/>
                <Root level="ERROR">
                        <AppenderRef ref="A" />
                        <AppenderRef ref="R" />
                </Root>
        </Loggers>
</Configuration>

Configuring Log4j 1.2

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