[英]How to correctly use log4j2 in Spring Boot application
我只想在我的应用程序中包含 log4j2 用于日志记录目的。 所以我在我的项目的 pom.xml 中包含了以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
但是在应用程序开始时,我收到以下异常:
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]
在许多平台上研究更多关于它的信息时,我发现了一种排除 logback 依赖的方法,因为我认为我需要使用 log4j2。但是在排除那个 jar 之后,我也收到以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
所以为了解决这个问题:我也排除了 log4j-to-slf4j 依赖项。
最后它开始工作,但现在我看到大量与 Spring boot Internal 相关的日志消息。 例如 :
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'
我的 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 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>
还有 log4j2 属性,如:
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
仅将 log4j2 用于应用程序目的而不是 Spring Boot 需要如何或什么是正确的配置。
所以我确定了触发从 spring boot 打印意外消息的问题。 从 log4j2 配置中,我设置了以下内容
rootLogger.level = debug
此属性使 log4j2 打印 spring boot 设置为调试模式的所有内部消息也打印。
将其更改为以下内容:
rootLogger.level = info
解决了这个问题。
现在应用程序只打印那些级别大于info 的记录器。 所以现在,它不会打印不需要的日志消息。
我会说这不是问题,log4j2 工作正常,但我的配置设置不符合我的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.