如何在 Spring 引导应用程序中正确使用 log4j2

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 依赖的方法,因为我认为我需要这样做 在排除该 jar 后使用 log4j2.But 我也会收到以下错误:

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 内部引导更相关的日志消息。 例如:

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 引导。

我相信这是因为处理 spring 引导内部文件的 log4j2 记录器包含在您的配置中。

看看 here - 这是相关的定义。

从技术上讲,我对 log2j2 不是很熟悉,但它似乎支持 xInclude 指令

所以我确定了触发从 spring 引导打印意外消息的问题。 在 log4j2 配置中,我设置了以下

rootLogger.level = debug

这属性 使log4j2 打印spring 引导设置为调试模式的所有内部消息也打印。

将其更改为以下内容:

rootLogger.level = info

问题已解决。

现在应用程序只打印那些级别大于 info 的记录器。 所以现在,它不会打印不需要的日志消息。

我会说这不是问题,log4j2 工作正常,但我的配置设置不符合我的要求。