如何正确设置Spring Boot和log4j2? [英] How to set up Spring Boot and log4j2 properly?

查看:81
本文介绍了如何正确设置Spring Boot和log4j2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过log4j2正确设置Spring日志以及我拥有的其他依赖项(休眠,Netty,Mina等)?

How can I make Spring log properly via log4j2 along with the other dependencies I have (Hibernate, Netty, Mina, etc.)?

我尝试了许多不同的Thin和依赖项组合.但是我要么可以让Spring登录,要么什么都可以,或者除了Spring之外的所有东西.

I tried many different thins and dependency combinations. But I either can get Spring to log and nothing else, or everything but Spring.

在所有依赖项正确记录(但春季)的情况下,出现以下错误:

With all dependecies logging properly (but Spring) I get the following error:

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依赖项

This is my build.gradle dependencies

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'
}

然后,摆脱我的日志记录依赖关系,并在下面添加依赖项:

Then, getting rid of my logging dependencies and adding the dependecy below:

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

Spring日志记录将起作用,而其他deps日志记录将不起作用.

Spring logging will work, but the other deps logging will not.

    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以及我所拥有的其他依赖项正确登录?

So again, how can I get Spring to log properly via log4j2 along with the other dependencies I have?

推荐答案

Spring Boot 1.4.1内置并支持Log4J 2.6,但是您尝试使用2.7.不幸的是,这不起作用,因为在

Spring Boot 1.4.1 is built with and supports Log4J 2.6 but you're trying to use 2.7. Unfortunately that doesn't work as there's a breaking API change in 2.7 that was made in this commit. This is the cause of the NoSuchMethodError that you're seeing.

拖放回Log4J2 2.6.2应该可以解决此问题.另外,您可以升级到使用Log4J 2.7的Spring Boot 1.5.

Dropping back to Log4J2 2.6.2 should fix the problem. Alternatively, you could upgrade to Spring Boot 1.5 which uses Log4J 2.7.

这篇关于如何正确设置Spring Boot和log4j2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆