log4j2.xml example

log4j 2 logo

Some log4j2.xml examples, just for self-reference

P.S Tested with Log4j 2.11.2

1. ConsoleAppender

Logs to console.

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Appenders>
        <Console name="LogToConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
		<!-- avoid duplicated logs with additivity=false -->
        <Logger name="com.mkyong" level="debug" additivity="false">
            <AppenderRef ref="LogToConsole"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="LogToConsole"/>
        </Root>
    </Loggers>
</Configuration>

2. FileAppender

Logs to a file.

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Appenders>
        <File name="LogToFile" fileName="logs/app.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <!-- avoid duplicated logs with additivity=false -->
        <Logger name="com.mkyong" level="debug" additivity="false">
            <AppenderRef ref="LogToFile"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="LogToFile"/>
        </Root>
    </Loggers>
</Configuration>

3. ConsoleAppender + FileAppender

Logs to both console and file.

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Appenders>
        <Console name="LogToConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <File name="LogToFile" fileName="logs/app.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="com.mkyong" level="debug" additivity="false">
            <AppenderRef ref="LogToFile"/>
            <AppenderRef ref="LogToConsole"/>
        </Logger>
        <Logger name="org.springframework.boot" level="error" additivity="false">
            <AppenderRef ref="LogToConsole"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="LogToFile"/>
            <AppenderRef ref="LogToConsole"/>
        </Root>
    </Loggers>
</Configuration>

4 RollingFileAppender

Rotate log file. If file size > 10m or the current date is different with the log file time stamp, create archives.

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Appenders>
        <Console name="LogToConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingFile name="LogToRollingFile" fileName="logs/app.log"
                    filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
        <PatternLayout>
            <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="10 MB"/>
        </Policies>
    </RollingFile>

    </Appenders>
    <Loggers>
        <!-- avoid duplicated logs with additivity=false -->
        <Logger name="com.mkyong" level="debug" additivity="false">
            <AppenderRef ref="LogToRollingFile"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="LogToConsole"/>
        </Root>
    </Loggers>
</Configuration>

By default, it will create up to 7 archives on the same day.

sample
sample

We can override the default 7 archives with this DefaultRolloverStrategy


	<RollingFile name="LogToRollingFile" fileName="logs/app.log"
			filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
		<PatternLayout>
			<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
		</PatternLayout>
		<Policies>
			<TimeBasedTriggeringPolicy/>
			<SizeBasedTriggeringPolicy size="10 MB"/>
		</Policies>
		<DefaultRolloverStrategy max="10"/>
	</RollingFile>

5 RollingRandomAccessFileAppender

Similar to the RollingFileAppender, but faster.

log4j2.xml

	<RollingRandomAccessFile name="LogToRollingRandomAccessFile" fileName="logs/app.log"
			filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
		<PatternLayout>
			<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
		</PatternLayout>
		<Policies>
			<TimeBasedTriggeringPolicy/>
			<SizeBasedTriggeringPolicy size="1 MB"/>
		</Policies>
		<DefaultRolloverStrategy max="10"/>
	</RollingRandomAccessFile>

6 SMTPAppender

Need javax.mail to send email.

pom.xml

	<dependency>
		<groupId>com.sun.mail</groupId>
		<artifactId>javax.mail</artifactId>
		<version>1.6.2</version>
	</dependency>
log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <RollingRandomAccessFile name="LogToRollingRandomAccessFile" fileName="logs/app.log"
                filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingRandomAccessFile>

        <SMTP name="LogToMail" subject="Error Log From Log4j"
              from="from@DOMAIN"
              to="to@DOMAIN"
              smtpHost="smtp.mailgun.org"
              smtpPort="25"
              smtpUsername="abc"
              smtpPassword="123"
              bufferSize="100">
        </SMTP>

    </Appenders>
    <Loggers>
        <Logger name="com.mkyong" level="debug" additivity="false">
            <AppenderRef ref="LogToRollingRandomAccessFile"/>
            <AppenderRef ref="LogToConsole"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="LogToMail"/>
        </Root>
    </Loggers>
</Configuration>

Sample email

sample email

Download Source Code

References

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Samsam Recent comment authors
newest oldest most voted
sam
Guest
sam

Hey mkyong, great article. I implemented the above Log4j2 xml configuration onto my project. Now everytime I run the test, the logs are just added to the file instead of overwriting. Can you tell me, how to achieve that from your 3rd example of Log4j2

3. ConsoleAppender + FileAppender

Sam
Guest
Sam

Nice article to get the feet wet. But Shouldn’t there be some example of properties attribute as well and how to read them in JAVA?