Sunday, 13 March 2022

Spring boot: log4j2: hello world application

In this post, I am going to explain how to setup a spring boot + log4j2 application in Eclipse.

 

Step 1: Create new maven project ‘log4j-hello-world’ in eclipse.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>log4j-hello-world</artifactId>
	<version>1</version>

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


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</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-log4j2</artifactId>
		</dependency>
	</dependencies>

</project>

 

Step 3: create com.sample.app.controller package and define HomeController class.

 

HomeController.java

 

package com.sample.app.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

	private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);

	@RequestMapping("/")
	public String home() {

		LOGGER.debug("Debug message");
		LOGGER.info("Information message");
		LOGGER.warn("Warning message");
		LOGGER.error("Error message");

		return "Hello World";
	}
}

 

Step 4: Define main application class.

 

App.java

 

package com.sample.app;

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

@SpringBootApplication
public class App {
	public static void main(String[] args) {

		SpringApplication.run(App.class, args);

	}
}

 

Create log4j2-spring.xml file under src/main/resources folder.

 

log4j2-spring.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
	<Properties>
		<Property name="LOG_PATTERN">
			%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36}.class Line:%L
			%M()-User:%X{userName} - %msg%xEx%n
		</Property>
	</Properties>
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">
			<PatternLayout pattern="${LOG_PATTERN}" />
		</Console>
	</Appenders>
	<Loggers>
		<Root level="ERROR">
			<AppenderRef ref="Console" />
		</Root>
		<logger name="com.sample.app" level="INFO" />
		<logger name="org.springframework" level="ERROR" />
		<logger name="springfox.documentation" level="ERROR" />
		<logger name="springfox.documentation" level="ERROR" />
		<logger name="org.springframework.security" level="ERROR" />
	</Loggers>
</Configuration>

Total project structure looks like below.



Run App.java and hit the url ‘http://localhost:8080/’, you will see below messages in console.

2022-02-17 22:08:07.682  INFO 14648 --- [nio-8080-exec-1] c.sample.app.controller.HomeController   : Information message
2022-02-17 22:08:07.682  WARN 14648 --- [nio-8080-exec-1] c.sample.app.controller.HomeController   : Warning message
2022-02-17 22:08:07.682 ERROR 14648 --- [nio-8080-exec-1] c.sample.app.controller.HomeController   : Error message

 

Since I configured log level as INFO for the package com.sample.app, debug message is not logged here.

 

You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/log4j2/log4j-hello-world

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment