【问题标题】:error: cannot find symbol Logger logger = Logger.getLogger(this.getClass().getName()) after Spring 1.5.9 upgrade to 2.0.1错误:Spring 1.5.9 升级到 2.0.1 后找不到符号 Logger logger = Logger.getLogger(this.getClass().getName())
【发布时间】:2018-10-19 13:33:39
【问题描述】:

我已将 Gradle 构建脚本更改为使用 Spring 2.0.1 而不是 1.5.9。当我运行gradle build 时,我得到error: cannot find symbol Logger logger = Logger.getLogger(this.getClass().getName())。它与以前的 Spring Boot 版本配合得很好。代码使用import org.apache.log4j.Logger;。如何解决这个问题?

build.gradle文件:

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

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

sourceCompatibility = 1.8

 repositories {
    mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }

bootRun {
    sourceResources sourceSets.main
}

sourceSets {
    main {
        java {
            srcDirs = ["src/main/java", "src/generated/main/java"]
        }
    }
}

dependencies {

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

    providedRuntime group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat'

    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework.boot:spring-boot-starter-velocity:1.4.7.RELEASE'

    compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
    compile 'com.ryantenney.metrics:metrics-spring:3.1.3'

    compile 'com.github.ben-manes.caffeine:caffeine:2.6.2'
    compile 'org.hibernate:hibernate-java8'
    compile 'org.postgresql:postgresql'
    compile 'org.apache.commons:commons-lang3:3.5'
    compile 'commons-codec:commons-codec:1.9'
    compile 'io.springfox:springfox-swagger2:2.6.1'
    compile 'io.springfox:springfox-swagger-ui:2.6.1'
    compile 'javax.mail:mail:1.4.7'
    compile 'org.imgscalr:imgscalr-lib:4.2'
    compile 'com.restfb:restfb:1.37.0'
    compile 'com.google.apis:google-api-services-oauth2:v2-rev134-1.23.0'
    compile 'eu.bitwalker:UserAgentUtils:1.19'
    compile 'com.twilio.sdk:twilio:7.17.+'

    testCompile('com.h2database:h2')
    testCompile("org.springframework.boot:spring-boot-starter-test")

    compile fileTree(dir: 'libs', include: '*.jar')
}

【问题讨论】:

  • 考虑到 Spring boot 默认使用 logback,而 log4j 1.x 的 Spring boot starter 只能到 1.3.x.RELEASE,我猜你自己包含了这些依赖项。你能发布你的 Gradle 文件吗?
  • 已发布 Gradle 文件。

标签: java spring spring-boot log4j


【解决方案1】:

默认情况下,Spring boot 提供 Logback 和 SLF4J 来进行日志记录,如 the documentation 中所述。但是,您可以通过包含以下依赖项将 logback 交换为 log4j:

  • log4j 1.x spring-boot-starter-log4j
  • log4j 2.x spring-boot-starter-log4j2

但是,对 log4j 1.x 的支持已被删除 since Spring boot 1.4.x,因为 Apache 也不再支持它:

Apache EOL announcement 之后删除了对 Log4j 1 的支持。

您仍然可以手动添加所有依赖项,但由于您没有这些,这可能是它不再工作的原因(也许其中一个库以前使用过 log4j)。您必须添加以下依赖项:

compile 'org.slf4j:slf4j-log4j12:1.7.25'
compile 'org.slf4j:jul-to-slf4j:1.7.25'
compile 'org.slf4j:jcl-over-slf4j:1.7.25'
compile 'log4j:log4j:1.2.17'

您还必须排除spring-boot-starter-logging,如this answer 中所述,您可以通过添加以下配置来做到这一点:

configurations {
    compile.exclude module: 'spring-boot-starter-logging'
}

但是,建议将 SLF4J 与 Logback 一起使用,或者将 SLF4J 与 log4j2 一起使用。

【讨论】:

    【解决方案2】:

    Spring Boot 2 默认使用 Logback 和 SLF4J。
    您可以选择使用网桥,它将 log4j 1 与 slf4j 连接起来,如下所示:

    compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: '1.7.25'
    

    但我建议您将代码切换为使用 slf4j(如果您的代码库很大,这可能需要一些时间)。

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 1970-01-01
      • 2018-08-26
      • 2018-09-16
      • 2023-03-29
      • 2023-02-24
      • 2020-11-24
      • 2019-11-18
      • 2021-11-30
      相关资源
      最近更新 更多