Log4j 无法启动spring启动服务器

Log4j 无法启动spring启动服务器,log4j,spring-boot,Log4j,Spring Boot,我是spring boot新手,当我尝试启动服务器时,出现以下异常。我知道这与依赖冲突有关,但仍然无法解决。我正在使用maven管理我的依赖项。请帮助 Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the c

我是spring boot新手,当我尝试启动服务器时,出现以下异常。我知道这与依赖冲突有关,但仍然无法解决。我正在使用maven管理我的依赖项。请帮助

 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.slf4j.impl.Log4jLoggerFactory) Object of class
 [org.slf4j.impl.Log4jLoggerFactory] must be an instance of class
 ch.qos.logback.classic.LoggerContext   at
 org.springframework.util.Assert.isInstanceOf(Assert.java:339)  at
 org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:93)
        at
org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSensibleDefaults(AbstractLoggingSystem.java:62)
        at

 org.springframework.boot.logging.AbstractLoggingSystem.beforeInitialize(AbstractLoggingSystem.java:45)
    at

org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:69)
    at

 org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:135)
    at

 org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98)
    at

 org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100)
    at

 org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:54)
    at

 org.springframework.boot.SpringApplication.run(SpringApplication.java:276)
    at

 org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at

 org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at org.magnum.mobilecloud.video.Application.main(Application.java:30)
已解决:将以下内容添加到POM.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
            <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
    </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
    </dependency>

org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动机tomcat
org.springframework.boot
spring启动程序日志记录
org.springframework.boot
spring-boot-starter-log4j

最终解决了这个问题,排除了Logback依赖项,并明确添加了log4j依赖项

排除了spring boot starter web中的Logback classic,并且spring boot starter执行器对我有效

compile("org.springframework.boot:spring-boot-starter-web:1.1.10.RELEASE") {
    exclude module: "spring-boot-starter-tomcat"
    exclude module: "spring-boot-starter-logging"
    exclude module: "logback-classic"
}
compile("org.springframework.boot:spring-boot-starter-actuator:1.1.10.RELEASE") {
    exclude module: "logback-classic"
}

将此添加到build.gradle中

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

在我的项目中,它使用

  • 弹簧靴1.4.2.1释放
  • slf4j 1.7.21和logback 1.1.7。(称为模块A的某些依赖关系依赖于logback 1.1.2,这就是问题所在)
  • 首先

    依赖项中介-这决定了当遇到工件的多个版本时,将使用哪个版本的依赖项。目前,Maven 2.0只支持使用“最近的定义”,这意味着它将使用依赖关系树中与项目最接近的依赖关系的版本。您总是可以通过在项目的POM中显式声明来保证版本。请注意,如果两个依赖项版本在依赖项树中处于相同的深度,则在Maven 2.0.8之前,没有定义哪一个版本将获胜,但由于Maven 2.0.9,则声明中的顺序才是最重要的:第一个声明获胜。 “最近的定义”意味着使用的版本将是依赖关系树中与您的项目最接近的版本,例如,如果A、B和C的依赖关系定义为A->B->C->D2.0和A->E->D1.0,那么在构建A时将使用D1.0,因为从A到D到E的路径较短。您可以在中显式地向D2.0添加依赖项,以强制使用D2.0

    所以maven将在我的项目中使用Logback1.1.7。我不确定是我的模块A与1.1.7不兼容还是logback 1.1.7与slf4j 1.7.21不兼容 不管怎样,对我来说。我在pom中添加DependencyManager。告诉maven仅使用lockback 1.1.2。问题解决了

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.1.2</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.1.2</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-access</artifactId>
                <version>1.1.2</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    
    回写
    回归经典
    1.1.2
    回写
    回溯堆芯
    1.1.2
    回写
    回写访问
    1.1.2
    
    Gradle解决方案正在build.Gradle中添加以下行:

    configurations {
        all*.exclude module : 'spring-boot-starter-logging'
    }
    

    我的gradle.build文件中的以下配置适用于我:

    configurations {
        all*.exclude group: "org.springframework.boot", module: "spring-boot-starter-logging"
        all*.exclude group: "ch.qos.logback"
        all*.exclude group: "org.slf4j", module: "log4j-over-slf4j" // allow using log4j 2.x
        all*.exclude group: "org.slf4j", module: "slf4j-simple"     // log4j is the configured backend
    }
    

    我建议您尝试删除任何包含Logback的依赖项,最常见的是:

    
    org.springframework.boot
    spring-boot-starter-log4j2
    

    这对我来说很有效。

    重复回答,但您可以使用Eclipse排除spring boot starter日志依赖项

  • 选择依赖关系层次结构
  • 搜索右上角的日志记录
  • 选择spring启动程序日志记录
  • 右键单击排除maven工件
  • e、 g

    如果在类路径上,Spring Boot支持Log4j 2进行日志配置。如果使用启动器组装依赖项,则必须排除Logback,然后包含log4j 2

    通过按此顺序添加以下依赖项解决了此问题

     <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    
     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
    
    org.springframework.boot
    spring-boot-starter-log4j2
    org.springframework.boot
    SpringBootStarterWeb
    org.springframework.boot
    弹簧启动机tomcat
    org.springframework.boot
    spring启动程序日志记录
    
    您能否提供您的
    pom.xml
    ?您好,最后通过排除Logback依赖项并显式添加log4j依赖项解决了这个问题。很高兴听到您解决了这个问题。回答你自己的问题没有坏处:你应该把你的解决方案作为答案贴出来,然后接受它。我对Spring Boot做了一个改进,这样它就可以确定竞争对手LoggerFactory实现的来源,并希望使问题更容易诊断:Andy,gr8。。。谢谢你,我知道这是一个老问题,但为了未来的读者:记住“mvn依赖:树”任务是你的朋友。它需要一些工作——例如,您必须认识到org.slf4j:slf4j simple与logback冲突——但是当遇到这个问题时(通常是由于可传递的依赖关系)以树状形式查看依赖项通常会使我找到解决方案的路径。您能分享gradle更改吗?我可以确认这很有帮助。适用于我的gradle配置与上面提到的相同。配置{all*.exclude group:“org.springframework.boot”,模块:“spring boot starter logging”all*.exclude group:“ch.qos.logback”all*.exclude group:“org.slf4j simple”//log4j是配置的后端}谢谢你,伙计,其他解决方案不起作用,但明确定义了!