I wanted to include log4j2 for the logging purpose in my application only. So I included the following dependency in the pom.xml of my project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
But at the start of the application I get the following exceptions :
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/m2repo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/m2repo/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
On researching more about it on many platforms I found an approach to exclude logback dependency as I thought I need to use the log4j2.But after excluding that jar also I get the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
So for solving that: I excluded the log4j-to-slf4j dependency also.
And finally it starts working but now I see a ton of log messages which are more related to Spring boot Internal. For instance :
2019-12-05 12:05:01.649 [DEBUG] [restartedMain] - Included patterns for restart : []
2019-12-05 12:05:01.649 [DEBUG] [restartedMain] - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
2019-12-05 12:05:01.649 [INFO ] [restartedMain] - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-12-05 12:05:01.649 [DEBUG] [restartedMain] - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3883fd6d
2019-12-05 12:05:01.666 [DEBUG] [restartedMain] - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2019-12-05 12:05:01.686 [DEBUG] [restartedMain] - Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory'
2019-12-05 12:05:02.037 [DEBUG] [restartedMain] - Creating shared instance of singleton bean 'propertySourcesPlaceholderConfigurer'
2019-12-05 12:05:02.043 [DEBUG] [restartedMain] - Creating shared instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator'
2019-12-05 12:05:02.055 [DEBUG] [restartedMain] - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2019-12-05 12:05:02.056 [DEBUG] [restartedMain] - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2019-12-05 12:05:02.059 [DEBUG] [restartedMain] - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
My pom.xml is like :
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.log4j2</groupId>
<artifactId>log4j2-check</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>log4j2-check</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
also log4j2 properties like :
name=PropertiesConfig
property.filename = Test
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] - %msg%n
rootLogger.level = debug
rootLogger.appenderRefs = STDOUT
rootLogger.appenderRef.stdout.ref = STDOUT
How can or what are correct configurations needed to use log4j2 only for application purposes and not for Spring boot.