Log4j 2 with Spring Boot example and logging hierarchy order

Hello everyone, today we learn how to use Apache Log4j 2 in the Spring Boot framework. Before going into the example let me share with you the visualization of logging hierarchy.

Going down the first column, you will see how the log works in each level. i.e for ERROR, (FATAL AND ERROR) will be visible, for WARN, (FATAL, ERROR and WARN) will be visible. I hope you all understood the logging hierarchy.

Let's begin our core context. i.e is how to use Log4j 2 in the spring boot framework.

Project Structure


Maven[pom.xml]

<?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>

<artifactId>logging-log4j2-knf</artifactId>
<packaging>jar</packaging>
<name>Spring Boot log4j2 Example</name>
<url>https://www.knowledgefactory.net</url>
<version>1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>

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

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

<!-- exclude logback , add log4j2 -->
<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>

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

<!-- asynchronous loggers -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>

</plugins>
</build>
</project>

log4j2.xml

Create a log4j2.xml in the project class path, src/resources/

<?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="logfiles/log.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="com.knowledgefactory" 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>

RestController[HelloController.java]

A simple Rest Controller application, logging with log42j

package com.knowledgefactory;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

private static final Logger logger = LogManager.getLogger
(HelloController.class);

@GetMapping("/test")
public String main(Model model) {

logger.trace("trace mode on");
logger.debug("debug mode on");
logger.info("info mode on");
logger.warn("warn mode on");
logger.error("error mode on");
logger.fatal("fatal mode on");

return "you are done";
}

}

application. properties

logging.level.com.knowledgefactory=INFO
#logging.level.=ERROR
logging.level.org.springframework=INFO

StartApplication.class - Start Spring Boot

package com.knowledgefactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartApplication {

public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}

}

call http://localhost:8080/test HTTP endpoint



Review the logging :

19:02:07.724 [http-nio-8080-exec-1] INFO com.knowledgefactory.HelloController - info mode on 19:02:07.725 [http-nio-8080-exec-1] WARN com.knowledgefactory.HelloController - warn mode on 19:02:07.725 [http-nio-8080-exec-1] ERROR com.knowledgefactory.HelloController - error mode on 19:02:07.725 [http-nio-8080-exec-1] FATAL com.knowledgefactory.HelloController - fatal mode on

Inside our application. properties we declared logging.level.com.knowledgefactory=INFO
i.e (FATAL, ERROR, WARN, INFO) will be visible. I hope you understood the system.

Download source code

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Top 5 Java ORM tools - 2024

Java - How to Count the Number of Occurrences of Substring in a String