Introduction

In this tutorial I will show you how to configure log4j2 in Java, Spring and Spring Boot applications. The same log4j2 configuration will work for Java, Spring and Spring Boot applications. I am not going to show you how to use logback API in Spring Boot application, instead I will exclude logback API from the Spring Boot application.

I am going to use log4j2 with slf4j for Java, Spring and Spring Boot applications. You can even use spring-boot-starter-log4j2 API instead of log4j2 with slf4j for Spring Boot application. This starter dependency will resolve the similar kind of libraries as log4j2 with slf4j.

Prerequisites

At least Java 8, log4j2 with slf4j API, Spring, Spring Boot

Create Build Configuration

If you are creating gradle based project in Eclipse then you can use the following dependencies in build.gradle script:

dependencies {
      implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.13.0'
}

If you are using maven based project then you can use below dependencies in pom.xml file:

<dependencies>
	<dependency>
		<groupId>org.apache.logging.log4j</groupId>
		<artifactId>log4j-slf4j-impl</artifactId>
		<version>2.13.0</version>
	</dependency>
</dependencies>

The above build configuration for gradle or maven based project will work for Java, Spring and Spring boot applications. But you need to exclude logback API from Spring Boot starter otherwise it will conflict and logback will override log4j2.

For gradle build:

configurations {
    all*.exclude module : 'spring-boot-starter-logging'
}

For maven build:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

For Spring Boot applications you can also use only log4j starter API that will resolve the similar libraries. Make sure to exclude the logback API even when you are using log4j starter API.

For gradle build:

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}

For maven build:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
</dependencies>

Create log4j2.xml

Log4j is a logging library for Java based applications and purpose of inserting log statements into code is a low-tech method for debugging it. But make sure you follow the best practices while putting log statements.

Learn Logging using logback in Spring Boot application.

Now we will create log4j2.xml file under the classpath directory src/main/resources.

As we are using log4j2 API, so the configuration file name is log4j2.xml or log4j2.properties. If you are using log4j API version 1, then your file name should be log4j.xml or log4j.properties.

Keeping the log4j2 XML or properties file in classpath will be picked up by the application automatically.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">

	<Properties>
		<Property name="LOG_LOC">logs</Property>
		<Property name="MAX">5</Property>
		<Property name="LOG_PATTERN">%d{yyyy.MM.dd HH:mm:ss.SSS} [%p] %c: %m%n
		</Property>
	</Properties>

	<Appenders>
		<RollingFile name="FILE" fileName="${LOG_LOC}/main.log"
			filePattern="${LOG_LOC}/main.%i.log">
			<PatternLayout>
				<Pattern>${LOG_PATTERN}</Pattern>
			</PatternLayout>

			<Policies>
				<OnStartupTriggeringPolicy />
			</Policies>

			<DefaultRolloverStrategy max="${MAX}" />
		</RollingFile>

		<Console name="STDOUT" target="SYSTEM_OUT" follow="true">
			<PatternLayout pattern="${LOG_PATTERN}" />
		</Console>
	</Appenders>

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

		<Logger name="file" level="debug" additivity="false">
			<appender-ref ref="FILE" />
		</Logger>

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

</Configuration>

The status in the log4j2 configuration is used internally by log4j2 components. This is useful for troubleshooting the configuration issues. Here I have turned this off. You can set debug, warn etc. for the status.

Then we set some properties with key/value pairs.

Next we define two appenders – RollingFile and Console.

In RollingFile appender we set the file name and file name pattern. We have maximum backup file 5 apart from main.log file. The file name pattern will create main.1.log … main.5.log and another main.log file will be the current log file.

We have specified the pattern of the log statements.

Next we have specified policy to start a new log file whenever the application starts up. During start up the file name pattern comes into picture and maximum 5 files will be created apart from main log file.

The console appender prints the log statements into console.

Next we define some loggers with level of logging.

The root level of logging is info for file as well as console logging.

Avoid duplicate logging by adding aditivity=”false”.

Thanks for reading.

Tags:

Leave a Reply

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