【问题标题】:Logging Error Spring boot Gradle记录错误 Spring Boot Gradle
【发布时间】:2018-07-13 13:12:05
【问题描述】:

当我尝试在我的应用程序中登录时出现以下错误

    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58)
Caused by: 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 jar:file:/home/dev/code/my_proj/build/libs/proj-core-0.1.0.jar!/BOOT-INF/lib/log4j-slf4j-impl-2.6.2.jar!/). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.apache.logging.slf4j.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext
        at org.springframework.util.Assert.isInstanceOf(Assert.java:346)
        at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartedEvent(LoggingApplicationListener.java:226)
        at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:205)
        at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:62)
        at org.springframework.boot.SpringApplicationRunListeners.started(SpringApplicationRunListeners.java:48)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
        at com.abc.def.Application.main(Application.java:22)
        ... 8 more

这是我的 logback.xml

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <encoder>
            <pattern>%date{YYYY-MM-dd HH:mm:ss} [%.7thread] %level %logger{10}:%line %mdc%n %msg%n</pattern>
        </encoder>


    </appender>

    <logger name="com.abc" level="debug"
            additivity="false">
        <appender-ref ref="STDOUT"/>
    </logger>
    <root level="ERROR">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

这是我在 build.gradle 中的依赖项

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-mongodb")
    compile ('org.springframework.boot:spring-boot-starter-web'){
        exclude module: 'org.springframework.boot:spring-boot-starter-logging'
    }
    compile ('org.springframework.boot:spring-boot-starter-log4j2')
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-mail")
    compile("org.springframework.boot:spring-boot-starter-amqp")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    compile group: 'org.springframework', name: 'spring-context-support'

    compile 'org.codehaus.groovy:groovy'
}

我使用它们如下。

import org.apache.log4j.Logger;
@Service
public class MyService {
  Logger logger = Logger.getLogger(MyService.class.getName());

我的应用程序甚至没有启动 我尝试了谷歌搜索,但它非常令人困惑,因为我是 Gradle 和日志记录的新手。请帮忙。

【问题讨论】:

    标签: spring-boot logging gradle


    【解决方案1】:

    您有两个带有 Logback 实现的 jar,它们正在竞争成为活动的,如 Caused by 消息中所述,该消息也提出了一个解决方案:删除 log4j-slf4j-impl-2.6.2.jar

    Caused by: 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 jar:file:/home/dev/code/my_proj/build/libs/proj-core-0.1.0.jar!/BOOT-INF/lib/log4j-slf4j-impl-2.6.2.jar!/). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.apache.logging.slf4j.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext

    【讨论】:

      【解决方案2】:

      原因:java.lang.IllegalArgumentException:LoggerFactory 不是 Logback LoggerContext,但 Logback 在类路径上。删除 Logback 或竞争实现(从 jar:file:/home/dev/code/my_proj/build/libs/proj-core-0.1.0.jar!/BOOT- 加载的类 org.apache.logging.slf4j.Log4jLoggerFactory INF/lib/log4j-slf4j-impl-2.6.2.jar!/)。如果您使用的是 WebLogic,则需要将“org.slf4j”添加到 WEB-INF/weblogic.xml 中的 prefer-application-packages 类 [org.apache.logging.slf4j.Log4jLoggerFactory] ​​的对象必须是 ch 类的实例.qos.logback.classic.LoggerContext

      如果在 SpringBoot 中尝试使用 log4j2 时出现此错误,那么 执行以下步骤:

      • 通过添加此“excludeGroupIds log4j-slf4j-impl /excludeGroupIds”从打包时删除 jar
      • 使用命令“mvndependecy:tree”找出哪个库依赖于“logback-classic”
      • 无论您在哪里找到它,都将其从依赖项中排除。

      发生此错误是因为 logback 覆盖了 log4j2 更改。所以如果你想使用 log4j2 那么你必须删除 logback 库和依赖项。

      希望这会对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-07
        • 2018-06-26
        • 2016-09-17
        • 2021-11-01
        • 1970-01-01
        • 2020-06-30
        • 1970-01-01
        相关资源
        最近更新 更多