Log4j2 with log4j2.xml Configuration Example

Project structure

In the following tutorial we will show you how to configure log4j2 with log4j2.xml file. We will show you how to print the logging to the console and configure log4j2 to print the logging to a file.

+--src
|   +--main
|       +--java
|           +--com
|               +--memorynotfound
|                   +--logging
|                       |--Log4j2Example.java
|       +--resources
|           |--log4j2.xml
pom.xml

Maven Dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.logging.log4j</groupId>
    <artifactId>xml</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>LOG4J2 - example xml file</name>

    <properties>
        <log4j2.version>2.1</log4j2.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
    </dependencies>

</project>

Example log4j2.xml file

Create a log4j2.xml file and put it into the resources folder.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">

    <Appenders>
        <File name="FILE" fileName="logfile.log" append="true">
            <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n"/>
        </File>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="com.memorynotfound" level="debug"/>

        <Root level="info">
            <AppenderRef ref="STDOUT"/>
            <AppenderRef ref="FILE"/>
        </Root>
    </Loggers>

</Configuration>

We will not explain the pattern used here. To learn more read the Log4j2 pattern layout guide.

Note: In the above configuration the default log level is INFO. This means that every log level greater or equal than INFO will be printed to log output. But we defined a log level package logger for our project which defines level DEBUG which will override the root logger.

Testing log4j2 configuration

package com.memorynotfound.logging;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4j2Example {

    private static final Logger LOG = LogManager.getLogger(Log4j2Example.class);

    public static void main(String... args){
        LOG.debug("This will be printed on debug");
        LOG.info("This will be printed on info");
        LOG.warn("This will be printed on warn");
        LOG.error("This will be printed on error");
        LOG.fatal("This will be printed on fatal");

        LOG.info("Appending string: {}.", "Hello, World");
    }
}

Output

As said in the note we override the root logger with a package logger equal to DEBUG as such, all levels greater or equal than debug will be printed.

DEBUG | 2015-01-03 15:30:32 | [main] logging.Log4j2Example (Log4j2Example.java:11) - This will be printed on debug
INFO  | 2015-01-03 15:30:32 | [main] logging.Log4j2Example (Log4j2Example.java:12) - This will be printed on info
WARN  | 2015-01-03 15:30:32 | [main] logging.Log4j2Example (Log4j2Example.java:13) - This will be printed on warn
ERROR | 2015-01-03 15:30:32 | [main] logging.Log4j2Example (Log4j2Example.java:14) - This will be printed on error
FATAL | 2015-01-03 15:30:32 | [main] logging.Log4j2Example (Log4j2Example.java:15) - This will be printed on fatal
INFO  | 2015-01-03 15:30:32 | [main] logging.Log4j2Example (Log4j2Example.java:17) - Appending string: Hello, World.

References

You may also like...

  • NathanR

    Hi,
    I get the console logs but not the file.
    Any idea?
    Where is the file saved? maybe I’m just not seeing it?

    Thanks

    • By default, when you don’t specify the full path, the log file is saved at the root of your project. You can optionally specify the file-path in the fileName attribute. For example:
      – mac/linux/unix users can add a path like “/tmp/logfile.log”
      – windows users can add a path like “C:tmplogfile.log”