Monday, 14 March 2022

Specify the location of log4j2.xml file in a spring boot application

There are two possible solutions here.

 

Solution 1: By specifying the environment property 'logging.config', we can provide the custom location of log4j2 file.

 

Example

java -Dlogging.config='/path/to/log4j2.xml' -jar spring-app.jar

 

Solution 2:

Define 'log4j2.component.properties' file in resources folder and define the property 'log4j.configurationFile'

 

Example 1 if the log4j2.xml file is in classpath.

log4j.configurationFile=classpath:log4j2.xml

 

Example 2: If the og4j2.xml file is in some other location.

log4j.configurationFile={full_file_path}

 

Find the below working application.

 

Step 1: Create new maven project ‘spring-custom-log4j-location’.

 

Step 2: Update pom.xml file 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>spring-custom-log4j-location</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: Define log4j2.component.properties file under src/main/resources folder.

 

log4j2.component.properties

log4j.configurationFile=/Users/Shared/log4j2-spring.xml

 

Define log4j2-spring.xml file in /Users/Shared folder (you can change this location).

 

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>

Step 4: 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 5: 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);

	}
}

Total project structure looks like below.




 

 

 

Run App.java.

 

Hit the url ‘http://localhost:8080/’ in browser, you will see below messages in console.

 

 

2022-02-19 19:15:23.513  INFO 9275 --- [nio-8080-exec-1] c.sample.app.controller.HomeController   : Information message
2022-02-19 19:15:23.513  WARN 9275 --- [nio-8080-exec-1] c.sample.app.controller.HomeController   : Warning message
2022-02-19 19:15:23.513 ERROR 9275 --- [nio-8080-exec-1] c.sample.app.controller.HomeController   : Error message

You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/log4j2/spring-custom-log4j-location

 

 


 



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment