我尝试将Spring的默认记录器更改为log4j2有什么问题? [英] What's wrong with my attempt to change Spring's default logger to log4j2?

查看:71
本文介绍了我尝试将Spring的默认记录器更改为log4j2有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring Boot的新手,我想将默认记录器更改为log4j2,因为它具有比logback更高的吞吐率。

I'm a neophyte to Spring Boot, and I wanted to change my default logger to log4j2, since it has a higher throughput rate than logback.

这是我的Gradle脚本。正如您所看到的,我正在使用Spring Boot 2.0.3,并禁用默认记录器,我在Spring Boot Web之后使用了排除模块(logback和spring boot starter logger)。我正在编写脚本底部的log4j。

Here's my Gradle script. As you can see, I am using Spring Boot 2.0.3, and to disable the default logger I used exclude module(logback and spring boot starter logger) after Spring Boot Web. I am compiling log4j at the bottom of my script.

buildscript {
    ext {
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}



apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8



repositories {
    mavenCentral()
    jcenter()
}


ext {
    vaadinVersion = '8.4.1'
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web') {
        exclude module: "spring-boot-starter-logging"
        exclude module: "logback-classic"
    }
    compile('org.springframework.boot:spring-boot-starter-webflux')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    compile('com.vaadin:vaadin-spring-boot-starter')
    runtime('org.springframework.boot:spring-boot-devtools')
    compileOnly('org.springframework.boot:spring-boot-configuration-processor')
    compileOnly('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('io.projectreactor:reactor-test')
    testCompile('org.springframework.security:spring-security-test')
    testCompile 'junit:junit:4.12'


    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.1'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.1'
    // https://mvnrepository.com/artifact/com.h2database/h2
    //compile group: 'com.h2database', name: 'h2', version: '1.0.60'
    testCompile group: 'com.h2database', name: 'h2', version: '1.3.148'





}

dependencyManagement {
    imports {
        mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
    }
}

这是我的Spring Boot应用程序。

This is my Spring Boot application.

package app.clothapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ClothappApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClothappApplication.class, args);
    }
}

然而,当我运行Spring Boot应用程序时,它为我提供了一个AbstractMethodError。我已经阅读了其他一些SO问题,显然有些课程自上次编译以来发生了不适当的变化。任何人都可以提供一些帮助吗?

However, when I run my Spring Boot application, it provides me with an AbstractMethodError. I've read some other SO questions, and apparently some class has inappropriately changed since it was last compiled. Could anyone provide some help?

Exception in thread "main" java.lang.AbstractMethodError: org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(Lorg/apache/logging/log4j/core/config/ConfigurationSource;)Lorg/apache/logging/log4j/core/config/Configuration;
    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:472)
    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:442)
    at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:254)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:419)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:138)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:147)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:175)
    at org.apache.commons.logging.LogFactory$Log4jLog.<clinit>(LogFactory.java:199)
    at org.apache.commons.logging.LogFactory$Log4jDelegate.createLog(LogFactory.java:166)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:109)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:99)
    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:198)
    at app.clothapp.ClothappApplication.main(ClothappApplication.java:10)

谢谢。

推荐答案

spring-boot-starter-logging 将有多个传递依赖。例如:

There will be more than one transitive dependency on spring-boot-starter-logging. For example:


  • spring-boot-starter-security 取决于 spring-boot-starter

  • spring-boot-starter 取决于 spring-boot-starter-logging

  • spring-boot-starter-security depends on spring-boot-starter
  • spring-boot-starter depends on spring-boot-starter-logging

您可以运行 gradle依赖项确认。

为了从任何地方逃避依赖,请使用:

In order to exlcude a dependency from everywhere, use:

configurations {
    all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}






Spring建议包括对<$ c $的依赖c> spring-boot-starter 然后从那里排除记录器,例如:


Spring suggest including the dependency on spring-boot-starter and then excluding the logger from there, something like:

compile('org.springframework.boot:spring-boot-starter') {
    exclude module: "spring-boot-starter-logging"
}

但我发现并非所有春天依赖关系是如此良好的行为,所以当有其他地方的Spring记录显式依赖时,上述有时会失败。

But I have found that not all Spring dependencies are so well behaved, so the above sometimes fails when there are explicit dependencies on Spring logging elsewhere.

这篇关于我尝试将Spring的默认记录器更改为log4j2有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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