背景

将 -Dlog4j.debug 添加到IDEA的类的启动配置中
在这里插入图片描述
在这里插入图片描述
运行上图代码,这里log4j2.xml控制的日志级别是info,很明显是没生效。
在这里插入图片描述

DEBUG StatusLogger org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!
DEBUG StatusLogger Using ShutdownCallbackRegistry class org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry
WARN StatusLogger Multiple logging implementations found: 
Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory, Weighting: 10
Factory: org.apache.logging.slf4j.SLF4JLoggerContextFactory, Weighting: 15
Using factory: org.apache.logging.slf4j.SLF4JLoggerContextFactory

分析

警告信息显示检测到多个日志实现,权重分别为10和15。
最终选择了权重较高的org.apache.logging.slf4j.SLF4JLoggerContextFactory 作为日志的实现工厂。
现在需要让应用程序使用 Log4jContextFactory 而不是 SLF4JLoggerContextFactory 作为日志的实现工厂。因此需要检查依赖关系,移除冲突的依赖项(这里需要处理的依赖项是log4j-to-slf4j)。

处理

在这里插入图片描述
这里需要处理的是spring-boot-starter-web依赖中的log4j-to-slf4j。

<exclusions>
    <exclusion>
        <artifactId>log4j-to-slf4j</artifactId>
        <groupId>org.apache.logging.log4j</groupId>
    </exclusion>
</exclusions>

验证

在这里插入图片描述
在这里插入图片描述

运行代码没问题,配置的日志文件也有内容了。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐