简体   繁体   中英

error: cannot find symbol Logger logger = Logger.getLogger(this.getClass().getName()) after Spring 1.5.9 upgrade to 2.0.1

I have changed Gradle build script to use Spring 2.0.1 instead 1.5.9. When I run gradle build , I get error: cannot find symbol Logger logger = Logger.getLogger(this.getClass().getName()) . It worked well with previous Spring Boot version. Code uses import org.apache.log4j.Logger; . How to solve this problem?

build.gradle file:

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

By default, Spring boot provides Logback and SLF4J to do logging, as mentioned in the documentation . However, there you can swap logback for log4j by including the following dependency:

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

However, support for log4j 1.x has been dropped since Spring boot 1.4.x , as its no longer supported by Apache either:

Log4j 1 support has been removed following Apache EOL announcement .

You can still manually add all the dependencies, but since you don't have those, that's probably the reason why it's no longer working (perhaps one of those libraries used log4j before). You have to add the following dependencies:

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'

You also have to exclude spring-boot-starter-logging , as mentioned in this answer , you can do that by adding the following configuration:

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

However, it's recommended to use SLF4J with Logback, or SLF4J with log4j2.

Spring Boot 2 by default uses Logback and SLF4J.
You can choose to use a bridge, that will connect log4j 1 with slf4j like this:

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

But I'd advise you to switch your code to use slf4j (which might take some time, if your codebase is big).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM