【问题标题】:Changing JHipster to use log4j2更改 JHipster 以使用 log4j2
【发布时间】:2014-11-11 19:23:59
【问题描述】:

我已经构建了一个示例 Jhipster 应用程序。我想做的第一件事是将默认日志记录更改为使用 log4j2。

我已更新应用的 spring-boot 属性以指向版本 1.2.0 M2。

环顾四周,我发现了一些排除 logback 并包含 log4j2 的方法示例,但我没有取得太大进展。我设法包含了 log4j2,但它仍在寻找对 logback 的引用。我相信我可能需要更改为我生成的一些样板代码 jhipster,但是在尝试一次学习很多东西时,我不确定最好的方法。

这是我的 gradle 文件的一部分,我试图排除日志记录(根据我所做的其他谷歌搜索),并插入 log4j2:

compile group: 'com.mattbertolini', name: 'liquibase-slf4j', version: liquibase_slf4j_version
compile group: 'org.springframework.boot', name: 'spring-boot-actuator', version: spring_boot_version
compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2', version:spring_boot_version
compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: spring_boot_version
compile group: 'org.springframework.boot', name: 'spring-boot-loader-tools', version: spring_boot_version
compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version: spring_boot_version
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: spring_boot_version
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: spring_boot_version
compile group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: spring_boot_version
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: spring_boot_version) {
    exclude (module: 'spring-boot-starter-logging')
    exclude (module: 'spring_boot-starter-tomcat')
}
compile group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: spring_boot_version
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: spring_boot_version
compile group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector', version: spring_cloud_version
compile group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector', version: spring_cloud_version
compile group: 'org.springframework.cloud', name: 'spring-cloud-localconfig-connector', version: spring_cloud_version
compile(group: 'org.springframework', name: 'spring-context-support') {
    exclude(module: 'quartz')
}

这样做我在尝试启动我的应用程序时遇到了一些奇怪的错误:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/sssn/.gradle/caches/modules-2/files2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.0.2/f2cc4647e51810c3dd11df832229fac01d41aa51/log4j-slf4j-impl-2.0.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/sssn/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.1.2/b316e9737eea25e9ddd6d88eaeee76878045c6b2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/C:/Users/jmcgaffigan/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.0.2/f2cc4647e51810c3dd11df832229fac01d41aa51/log4j-slf4j-impl-2.0.2.jar). Object of class [org.apache.logging.slf4j.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext

如果有人有任何提示或建议“试试这个”,我们将不胜感激。

更新: 运行 gradle 依赖项总是显示通过 boot-start-aop 包进入的日志记录:

+--- org.springframework.boot:spring-boot-starter-aop:1.2.0.M2
|    +--- org.springframework.boot:spring-boot-starter:1.2.0.M2
|    |    +--- org.springframework.boot:spring-boot:1.2.0.M2 (*)
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:1.2.0.M2 (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:1.2.0.M2
|    |    |    +--- org.slf4j:jcl-over-slf4j:1.7.7
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.7
|    |    |    +--- org.slf4j:jul-to-slf4j:1.7.7
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.7
|    |    |    +--- org.slf4j:log4j-over-slf4j:1.7.7
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.7
|    |    |    \--- ch.qos.logback:logback-classic:1.1.2
|    |    |         +--- ch.qos.logback:logback-core:1.1.2
|    |    |         \--- org.slf4j:slf4j-api:1.7.6 -> 1.7.7
|    |    +--- org.springframework:spring-core:4.1.1.RELEASE (*)
|    |    \--- org.yaml:snakeyaml:1.13
|    +--- org.springframework:spring-aop:4.1.1.RELEASE (*)
|    +--- org.springframework:spring-core:4.1.1.RELEASE (*)
|    +--- org.aspectj:aspectjrt:1.8.2
|    \--- org.aspectj:aspectjweaver:1.8.2
+--- org.springframework.boot:spring-boot-starter:1.2.0.M2 (*)

我尝试在 starter-aop 和启动包上进行排除,但仍然没有运气。

【问题讨论】:

    标签: spring-boot jhipster


    【解决方案1】:

    如果您想使用依赖排除,那么要成功排除某些内容,需要将排除应用于传递依赖于您尝试排除的模块的每个依赖项。这很快就会变得笨拙——您需要在几乎所有 Spring Boot 依赖项上声明排除。

    更好的选择是将排除应用于整个配置:

    configurations {
        compile.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    

    甚至是每个配置:

    configurations {
        all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    

    【讨论】:

    • 谢谢安迪。 all*.exclude 解决了我的问题。你先生是天赐之物
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2020-09-18
    • 2019-04-30
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多