[英]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. 我已将Gradle构建脚本更改为使用Spring 2.0.1而不是1.5.9。 When I run
gradle build
, I get error: cannot find symbol Logger logger = Logger.getLogger(this.getClass().getName())
. 当我运行
gradle build
, error: cannot find symbol Logger logger = Logger.getLogger(this.getClass().getName())
。 It worked well with previous Spring Boot version. 它与以前的Spring Boot版本一起使用时效果很好。 Code uses
import org.apache.log4j.Logger;
代码使用
import org.apache.log4j.Logger;
. 。 How to solve this problem?
如何解决这个问题呢?
build.gradle
file: 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')
}
By default, Spring boot provides Logback and SLF4J to do logging, as mentioned in the documentation . 默认情况下,Spring boot提供Logback和SLF4J进行日志记录,如文档中所述 。 However, there you can swap logback for log4j by including the following dependency:
但是,您可以通过包含以下依赖项来将logback交换为log4j:
spring-boot-starter-log4j
spring-boot-starter-log4j
spring-boot-starter-log4j2
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: 但是, 自Spring boot 1.4.x以来 ,对log4j 1.x的支持已被删除, 因为 Apache不再支持它:
Log4j 1 support has been removed following Apache EOL announcement .
在Apache EOL发布后, Log4j 1支持已被删除。
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). 您仍然可以手动添加所有依赖项,但是由于没有依赖项,因此这可能是它不再起作用的原因(也许其中一个库以前使用过log4j)。 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: 您还必须排除
spring-boot-starter-logging
,如本答案中所述 ,您可以通过添加以下配置来做到这一点:
configurations {
compile.exclude module: 'spring-boot-starter-logging'
}
However, it's recommended to use SLF4J with Logback, or SLF4J with log4j2. 但是,建议将SLF4J与Logback一起使用,或者将SLF4J与log4j2一起使用。
Spring Boot 2 by default uses Logback and SLF4J. Spring Boot 2默认使用Logback和SLF4J。
You can choose to use a bridge, that will connect log4j 1 with slf4j like this: 您可以选择使用网桥,该网桥将log4j 1与slf4j连接,如下所示:
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). 但我建议您将代码切换为使用slf4j(如果您的代码库很大,则可能需要一些时间)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.