【问题标题】:How to set up Spring Boot and log4j2 properly?如何正确设置 Spring Boot 和 log4j2?
【发布时间】:2017-03-16 07:55:19
【问题描述】:

如何通过log4j2 以及我拥有的其他依赖项(Hibernate、Netty、Mina 等)正确地制作 Spring 日志?

我尝试了许多不同的细化和依赖组合。但我要么可以让 Spring 记录,要么只记录 Spring 之外的所有内容。

正确记录所有依赖项(但 Spring)我收到以下错误:

java.lang.NoSuchMethodError: org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(Lorg/apache/logging/log4j/core/config/ConfigurationSource;)Lorg/apache/logging/log4j/core/config/Configuration;
        at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadConfiguration(Log4J2LoggingSystem.java:167)
        at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadDefaults(Log4J2LoggingSystem.java:150)
        at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:75)
        at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:50)
        at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.initialize(Log4J2LoggingSystem.java:140)
        at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:277)
        at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:255)
        at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:224)
        at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:200)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:121)
        at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
        at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:329)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
        at com.myproject.MyProject.main(MyProject.java:XX)

这是我的 build.gradle 依赖项

dependencies {
    // FIX engine
    compile 'org.quickfixj:quickfixj-all:1.6.2'
    compile 'org.apache.mina:mina-core:2.0.16'

    // Web API
    compile ('org.springframework.boot:spring-boot-starter-web:1.4.1.RELEASE') {
        // Remove default Spring loggers (where logback takes precedence)
        exclude module: 'spring-boot-starter-logging'
    }

    // Logging
    compile 'org.apache.logging.log4j:log4j-api:2.7'
    compile 'org.apache.logging.log4j:log4j-core:2.7'
    compile 'commons-logging:commons-logging:1.2' // for Spring logging
    compile 'org.apache.logging.log4j:log4j-web:2.7' // for Spring logging
    compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.7' // for Hibernate logging

    // ORM and Database Driver
    compile 'org.hibernate:hibernate-core:5.2.4.Final'
    compile 'org.postgresql:postgresql:9.4.1209'

    // Key-value store
    compile 'biz.paluch.redis:lettuce:4.1.2.Final'

    // Testing
    // The Groovy language
    compile 'org.codehaus.groovy:groovy-all:2.4.6'
    // Spock testing framework
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}

然后,摆脱我的日志依赖并在下面添加依赖:

 compile 'org.springframework.boot:spring-boot-starter-log4j2:1.4.1.RELEASE'

Spring 日志记录可以工作,但其他部门的日志记录不会。

这是main 方法(没什么特别的)

    public static void main(String[] args) {
        // Fine tune a few logging settings so they don't log with TRACE or DEBUG levels.
        Configurator.setLevel("org.hibernate", Level.ERROR);
        Configurator.setLevel("org.jboss", Level.ERROR);
        Configurator.setLevel("org.apache.mina", Level.ERROR);
        Configurator.setLevel("io.netty", Level.ERROR);
        Configurator.setLevel("quickfix.mina", Level.WARN);
        Configurator.setLevel("com.lambdaworks", Level.WARN);

        // ...

        logger.info("FIX Controller started. Loading the API.");

        SpringApplication.run(MyProject.class, args);
    }

那么,我怎样才能让 Spring 通过 log4j2 以及我拥有的其他依赖项正确记录?

【问题讨论】:

    标签: java logging spring-boot log4j2


    【解决方案1】:

    Spring Boot 1.4.1 使用并支持 Log4J 2.6,但您尝试使用 2.7。不幸的是,这不起作用,因为在 this commit 中进行了 2.7 中的重大 API 更改。这就是您看到的NoSuchMethodError 的原因。

    退回到 Log4J2 2.6.2 应该可以解决问题。或者,您可以升级到使用 Log4J 2.7 的 Spring Boot 1.5。

    【讨论】:

    • 在所有 2.7 log4j2 依赖项上恢复到 2.6 已修复它!谢谢你。我认为org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration方法在2.6中是static,但在2.7中不是,从而破坏了API。
    • @K.C.是的!通过为 log4j 依赖项添加 2.7,我没有错误:)
    • 这是正确答案!这在我的情况下有效 - 即使我使用的是 1.2 桥接器,如果不将 Spring Boot 更新到最新的 1.5,它也无法工作。
    猜你喜欢
    • 2015-07-20
    • 2020-03-30
    • 2021-07-09
    • 2017-06-14
    • 2019-05-19
    • 2020-11-01
    • 2020-04-17
    • 2020-03-04
    • 2019-03-26
    相关资源
    最近更新 更多