【问题标题】:Logging with log4j2 in Spring Boot app在 Spring Boot 应用程序中使用 log4j2 进行日志记录
【发布时间】:2015-09-02 05:54:47
【问题描述】:

我正在尝试学习 Spring Boot。但是我完全弄乱了记录器的依赖关系。 我有一个简单的 pom:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>

我在 ..src\main\resources\ 中创建了 log4j2.xml

我用这样的东西登录:

private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);
LOG.error("ERRRRRRR!!!!");

但是当应用启动时我看到:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/me/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/me/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.3/log4j-slf4j-impl-2.3.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]

当然,log4j2 及其配置不起作用。

我知道,这是一个很受欢迎的问题。我用谷歌搜索了很多答案,但对我没有任何帮助。

【问题讨论】:

    标签: java spring maven logging log4j2


    【解决方案1】:

    您必须排除与 spring-boot-starter-batch 捆绑的 logback,并将其替换为 spring-boot-starter-log4j2。查看 documentation 的日志记录和 Spring Boot。

    这个 pom 配置很适合我。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <version>${spring.boot.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
    

    【讨论】:

      【解决方案2】:

      您的类路径中有多个 SLF4J 绑定。

      • Lo​​gback 经典,默认包含在 Spring Boot 中
      • 您要使用的 Log4j。

      你需要在类路径中有一个,通过使用 maven 依赖排除。

      【讨论】:

      • 我现在不确定。我是新手,刚刚开始学习所有这些。谢谢,我会继续使用 Logback。
      • 为此我在 POM 等中无事可做,Logback 是默认存在的,对吧?
      • 阅读 logback 文档以使用 logback 配置日志记录,并删除与 log4j 相关的依赖项。或阅读有关日志记录的 Spring Boot 文档。
      • Log4j 已经死了,但他想使用 log4j2,据我所知,它正在积极维护。
      猜你喜欢
      • 2017-12-20
      • 2014-10-30
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 2019-08-31
      • 2022-08-14
      • 2020-11-05
      • 2019-02-09
      相关资源
      最近更新 更多