2016-08-09 21 views
2

SpringBoot + Gradleプロジェクトでlog4jを有効にしようとするのは、Mavenよりも一般的ではありません。SpringBoot Gradleでlog4jを有効にする方法:log4j-over-slf4j.jar、slf4j-log4j12.jar複数のバインド

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 StackOverflowError. 
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details. 
Exception in thread "main" java.lang.ExceptionInInitializerError 

質問

のGradleを使用してそれを解決する方法

答えて

2

Gradleの依存関係の解決は

あなたは、ロードするライブラリのバージョンを解決するためのGradleの依存関係解決機能を使用することができます。エラーの1つは、ロードするLog4jのバージョンで、エラーログに示されている2つの場所からのものです。したがって、ライブラリの名前ごとに、どのバージョンを獲得するかを選択します。

2番目の点は、デフォルトでSpringBootのロギングを設定するspring-boot loggingとlogback classicへの依存関係を完全に除外することです。次のブロックがそれを助けました。

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の依存関係

最後に、追加するための最後の変更は、依存関係をロードするのに役立ちますlog4jslf4j依存関係のセットです。通常のディレクトリsrc/main/resources/

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

置き、それを。

これらの設定が完了したら、私は必要なものを手に入れました。ログの出力が異なり、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.spring[email protected]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 
関連する問題