如何在 Spring Boot 中启用 Log4j Gradle: log4j-over-slf4j.jar, slf4j-log4j12.jar Multiple Bindings
How to enable Log4j on SpringBoot Gradle: log4j-over-slf4j.jar, slf4j-log4j12.jar Multiple Bindings
尝试在 SpringBoot + Gradle 项目中启用 log4j
不如 Maven 常见。所以,这就是我面临的问题:
gradle clean bootRun --stacktrace
:clean
:compileJava
:processResources
:classes
:findMainClass
:bootRun
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/mdesales/.m2/repository/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/mdesales/.m2/repository/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting WhosebugError.
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
Exception in thread "main" java.lang.ExceptionInInitializerError
问题
- 如何用Gradle解决?
- 如何使用 Log4j 1.x 而不是 http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging 中记录的 Log4j2?
- 如何在 SpringBoot 中按原样重用现有的 log4j.xml 而无需更改?
Gradle 依赖解析
您可以使用Gradle的依赖解析能力来解析加载哪个库版本。其中一个错误与要加载的 Log4j 版本有关,它来自错误日志中显示的 2 个位置。因此,对于每个库的名称,select 哪个版本应该获胜。
它的第二部分是完全排除对 spring-boot logging 和 logback classic 的依赖,它们默认负责为 SpringBoot 设置日志记录。以下块对此有所帮助。
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.name == 'log4j') {
details.useTarget 'log4j:log4j:1.2.+'
}
}
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.springframework.boot', module: 'logback-classic'
}
Log4j 依赖项
最后,要添加的最后一个更改是一组 log4j
和 slf4j
依赖项,它们将帮助您加载依赖项。
runtime group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
runtime group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.21'
runtime group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.21'
runtime group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
runtime group: 'log4j', name: 'log4j', version: '1.2.17'
log4j.xml
放在常用目录下src/main/resources/
。
在这些到位之后,我得到了我需要的东西:日志输出不同并且使用了我 log4j.xml 上的属性。
$ SPRING_PROFILES_ACTIVE=dev gradle clean bootRun --stacktrace
:clean
:compileJava
:processResources
:classes
:findMainClass
:bootRun
2016-08-09 07:20:47,006 0 | INFO | context.annotation.AnnotationConfigApplicationContext.prepareRefresh#581 ["restartedMain" null] Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@15225245: startup date [Tue Aug 09 07:20:46 PDT 2016]; root of context hierarchy
2016-08-09 07:20:47,235 229 | INFO | internal.util.Version.<clinit>#30 ["background-preinit" null] HV000001: Hibernate Validator 5.2.4.Final
2016-08-09 07:20:47,440 434 | INFO | factory.annotation.AutowiredAnnotationBeanPostProcessor.<init>#155 ["restartedMain" null] JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
尝试在 SpringBoot + Gradle 项目中启用 log4j
不如 Maven 常见。所以,这就是我面临的问题:
gradle clean bootRun --stacktrace
:clean
:compileJava
:processResources
:classes
:findMainClass
:bootRun
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/mdesales/.m2/repository/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/mdesales/.m2/repository/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting WhosebugError.
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
Exception in thread "main" java.lang.ExceptionInInitializerError
问题
- 如何用Gradle解决?
- 如何使用 Log4j 1.x 而不是 http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging 中记录的 Log4j2?
- 如何在 SpringBoot 中按原样重用现有的 log4j.xml 而无需更改?
Gradle 依赖解析
您可以使用Gradle的依赖解析能力来解析加载哪个库版本。其中一个错误与要加载的 Log4j 版本有关,它来自错误日志中显示的 2 个位置。因此,对于每个库的名称,select 哪个版本应该获胜。
它的第二部分是完全排除对 spring-boot logging 和 logback classic 的依赖,它们默认负责为 SpringBoot 设置日志记录。以下块对此有所帮助。
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.name == 'log4j') {
details.useTarget 'log4j:log4j:1.2.+'
}
}
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.springframework.boot', module: 'logback-classic'
}
Log4j 依赖项
最后,要添加的最后一个更改是一组 log4j
和 slf4j
依赖项,它们将帮助您加载依赖项。
runtime group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
runtime group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.21'
runtime group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.21'
runtime group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
runtime group: 'log4j', name: 'log4j', version: '1.2.17'
log4j.xml
放在常用目录下src/main/resources/
。
在这些到位之后,我得到了我需要的东西:日志输出不同并且使用了我 log4j.xml 上的属性。
$ SPRING_PROFILES_ACTIVE=dev gradle clean bootRun --stacktrace
:clean
:compileJava
:processResources
:classes
:findMainClass
:bootRun
2016-08-09 07:20:47,006 0 | INFO | context.annotation.AnnotationConfigApplicationContext.prepareRefresh#581 ["restartedMain" null] Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@15225245: startup date [Tue Aug 09 07:20:46 PDT 2016]; root of context hierarchy
2016-08-09 07:20:47,235 229 | INFO | internal.util.Version.<clinit>#30 ["background-preinit" null] HV000001: Hibernate Validator 5.2.4.Final
2016-08-09 07:20:47,440 434 | INFO | factory.annotation.AutowiredAnnotationBeanPostProcessor.<init>#155 ["restartedMain" null] JSR-330 'javax.inject.Inject' annotation found and supported for autowiring