Spring 格雷德尔和;SLF4J拒绝排除可传递依赖项将导致IllegalStateException
在项目上运行单元测试时,我遇到此错误:Spring 格雷德尔和;SLF4J拒绝排除可传递依赖项将导致IllegalStateException,spring,gradle,log4j,slf4j,Spring,Gradle,Log4j,Slf4j,在项目上运行单元测试时,我遇到此错误: Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more det
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54)
... 29 more
我可以通过以下方式停止错误:
configurations {
all*.exclude group: "org.slf4j"
}
但是我在运行时得到了ClassNotFoundExceptions
,因为需要slf4j(及其适配器)
有什么想法吗?我只想从Spring的依赖项中排除slf4j。我在这个问题上看到了很多帖子,但没有解决它——我的排除是否正确?您的问题是,您拥有
log4j-over-slf4j
,它将所有log4j
日志重定向到slf4j
,和slf4j-log4j12
,将所有slf4j
日志输出到类路径中的log4j
。这意味着你会有一个无休止的循环,这当然是错误的
所以你必须决定你想要什么。是否要将所有log4j
日志重定向到slf4j
,然后将所有slf4j
日志定向到其他日志框架(或slf4j简单
,例如,用于登录到System.out
或不绑定以抑制日志记录),还是要将所有slf4j
日志定向到log4j
,那么,首先将log4j
日志重定向到slf4j
是没有意义的
如何正确解决问题在很大程度上取决于预期的结果。您还可以使用gradle任务
dependencyInsight
找出是什么拉入了依赖项,我刚刚排除了spring boot starter日志记录
:
configurations {
all*.exclude module: 'spring-boot-starter-logging'
}
我的依赖项现在如下所示:
dependencies {
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-jdbc:$springVersion"
compile "org.springframework:spring-orm:$springVersion"
compile "org.springframework:spring-tx:$springVersion"
compile "org.springframework.data:spring-data-jpa:1.10.1.RELEASE"
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
compile "org.apache.velocity:velocity:1.7"
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
testCompile("junit:junit")
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile group: 'junit', name: 'junit', version: '4.11'
}
我其实没有偏好,我只是想让它工作!让我们来看看log4j——我如何解决它呢?
configurations.all{exclude group:'org.slf4j',module:'slf4j-over-slf4j'}
btw。您不需要排除slf4j api
,如果您想要记录使用java commons日志的libs,您不应该排除jcl-over-slf4j
。当然,还要删除对log4j-over-slf4j
的显式依赖。然后添加slf4j-log4j12
并根据需要配置log4j
。谢谢您的帮助。我删除了spring boot starter日志记录,它把一切都整理好了。太好了,那么请阅读并遵守:-)我在下面添加了我的内容以获取更多信息,只是忘记接受你的。无意伤害:)
dependencies {
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-jdbc:$springVersion"
compile "org.springframework:spring-orm:$springVersion"
compile "org.springframework:spring-tx:$springVersion"
compile "org.springframework.data:spring-data-jpa:1.10.1.RELEASE"
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
compile "org.apache.velocity:velocity:1.7"
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
testCompile("junit:junit")
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile group: 'junit', name: 'junit', version: '4.11'
}