我已经将Gradle构建脚本改为使用Spring2.0.1而不是1.5.9。当我运行gradle build时,我得到了error: cannot find symbol Logger logger = Logger.getLogger(this.getClass().getName())。它与以前的Spring版本工作得很好。代码使用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')
}发布于 2018-05-09 12:53:55
默认情况下,Spring提供Logback和SLF4J来执行日志记录,就像文献资料中提到的那样。但是,可以通过包含以下依赖项将logback交换为log4j:
spring-boot-starter-log4jspring-boot-starter-log4j2但是,对log4j 1.x的支持已经取消,因为自Spring引导1.4.x以来不再支持它:
Log4j 1的支持在Apache公告之后被删除了。
您仍然可以手动添加所有依赖项,但由于没有这些依赖项,这可能是它不再工作的原因(可能是其中一个以前使用过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 (如这个答案中提到的那样),可以通过添加以下配置来做到这一点:
configurations {
compile.exclude module: 'spring-boot-starter-logging'
}但是,建议在Logback中使用SLF4J,或者在log4j2中使用SLF4J。
发布于 2018-05-09 12:53:43
Spring 2默认使用Logback和SLF4J。
您可以选择使用一个桥,它将log4j 1连接到slf4j,如下所示:
compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: '1.7.25'但我建议您将代码转换为使用slf4j (如果代码库很大,这可能需要一些时间)。
https://stackoverflow.com/questions/50253257
复制相似问题