In this post, we will look at Spring Boot Log4J2 configuration examples. This will build up from the previous post about the basic Spring Boot Log4J2 setup and therefore, it would be good to read this post after the first one.

If you are new to Spring Boot itself, I have a detailed series of posts about Spring Boot.

In the previous post, we had completed Log4J2 setup using basic parameters in the application.properties. The same setup can also be done using XML, YAML or JSON.

1 – Spring Boot Log4J2 XML Configuration

To configure Log4J2 using XML, we need to create a file named log4j2.xml in the resources folder of our application.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
        </Property>
    </Properties>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.progressivecoder.loggingdemo" level="debug" additivity="false">
            <AppenderRef ref="ConsoleAppender" />
        </Logger>

        <Root level="info">
            <AppenderRef ref="ConsoleAppender" />
        </Root>
    </Loggers>
</Configuration>

The important things to note here are as follows:

  • First we are defining a pattern for our logs. This is basically meant to transform how the logs should appear. As you can see, we have the timestamp of the log, hostname and the actual log details in the pattern
  • Next, we have the Appenders section. In this section, we define the ConsoleAppender and set the PatternLayout property to point to LOG_PATTERN.
  • After this, we have the Loggers section. In this section, we define the different types of Log Levels we want to use for a particular package. As you can see, we use the Log Level DEBUG for com.progressivecoder.loggingdemo package. Also, we link it to the Appender known as ConsoleAppender. This sends all logs of DEBUG level from this package to the console.
  • We also have a Root level Logger. It sends all logs with Log Level INFO to the console.

2 – Spring Boot Log4J2 YAML Configuration

In order to use YAML for configuring Spring Boot Log4J2, we need to have additional packages in our application. The packages are jackson-dataformat-yaml and jackson-databind.

Below are the updates in the POM.xml.

<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-yaml</artifactId>
			<version>2.11.3</version>
</dependency>
<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.11.3</version>
</dependency>

Next, we create a file with name log4j2.yml in the resources folder of our application. Below are the contents of the file.

Configutation:
  name: Default
  Properties:
    Property:
      name: log_pattern
      value: "%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex"
  Appenders:
    Console:
      name: Console_Appender
      target: SYSTEM_OUT
      PatternLayout:
        pattern: ${log_pattern}
  Loggers:
    Logger:
      - name: com.progressivecoder.loggingdemo
        level: debug
        additivity: false
        AppenderRef:
          - ref: Console_Appender
    Root:
      level: info
      AppenderRef:
        - ref: Console_Appender

The above configuration basically does the same thing as the previous XML configuration.

3 – Spring Boot Log4J2 JSON Configuration

We can also configure Log4J2 using JSON. To do so, first we update the POM.xml to include the below Jackson dependencies.

<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.11.3</version>
</dependency>
<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.11.3</version>
</dependency>
<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.11.3</version>
</dependency>

Next, we create a file name log4j2.json in the resources folder and define the configuration in that file. See below:

{
  "configuration": {
    "name": "Default",
    "properties": {
      "property": [
        {
          "name": "log_pattern",
          "value": "%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex"
        }
      ]
    },
    "appenders": {
      "Console": {
        "name": "Console-Appender",
        "target": "SYSTEM_OUT",
        "PatternLayout": {
          "pattern": "${log_pattern}"
        }
      }
    },
    "loggers": {
      "logger": {
        "name": "com.progressivecoder.loggingdemo",
        "level": "debug",
        "additivity" : "false",
        "appender-ref": [{"ref": "Console-Appender"}]
      },
      "root": {
        "level": "info",
        "appender-ref": {"ref": "Console-Appender"}
      }
    }
  }
}

This configuration also does the same thing as the XML and the YAML configurations respectively.


With this, we have looked at various way of performing Spring Boot Log4J2 Configuration. To be specific, we basically configured the Log4J2 settings using XML, YAML and JSON formats. The code for the XML configuration is available in Github. You can also read more about Apache Log4J2 at this link.

In the next post, we will look at File Appenders in Spring Boot Log4J2.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *