背景

项目中使用springboot,需要用log4j2做日志框架

问题

项目启动报错:Could not initialize Log4J2 logging from classpath:log4j2-dev.yml



是一个无法初始化Log4J2配置的问题,项目中采用的yml的配置文件。

前置操作

首先引入依赖:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

去掉默认的logback配置:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions><!-- 去掉默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

添加配置文件:

配置logging.config



以上是整合操作的必要配置,配置完成启动报错。

问题排查

根据异常信息描述,找到源码中初始化的代码:Log4J2LoggingSystem.loadConfiguration

继续跟进:

发现是一个抽象方法,idea中使用ctrl+alt+B查找实现类:

因为使用的是yml,所以实现类应该是YamlConfig这个,找到具体实现:

通过debug发现isActive是false,所以返回null。在此类中找到isActive的含义:

这是关键的逻辑,原来回去检查是否存在上面几个依赖的类,调试返现没有YAMLFactory这个类,可以看出这个类是jackson包中的,看来是少依赖了包。

解决

引入jackson-dataformat-yaml依赖:

    <dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

启动正常,问题解决。

最新文章

  1. 4.Rabbits and Recurrence Relations
  2. Linux C++ 调试神技--如何将Linux C++ 可执行文件逆向工程到Intel格式汇编
  3. Python sorted函数对列表排序
  4. C# txt格式记录时间,时间对比,决定是否更新代码记录Demo
  5. [PR &amp; ML 3] [Introduction] Probability Theory
  6. [原博客] POJ 2425 A Chess Game
  7. php拦截器(魔术方法)
  8. Zookeeper和 Google Chubby对比分析
  9. django模版中配置和使用静态文件方法
  10. docker 镜像运行问题
  11. MySQL如何查询多少行,多少列
  12. FFmpeg封装格式处理3-复用例程
  13. 我是如何沉迷于linux系统的?
  14. sqlserver2008r2实现镜像
  15. 题解 P2580 【于是他错误的点名开始了】
  16. Using a Microsoft Account to Logon and Resulting Internet Communication in Windows 8
  17. RHEL7.2安装部署redmine
  18. Java泛型的基本使用
  19. Zabbix使用SMTP发送邮件报警并且制定报警内容
  20. Jmter安装和配置

热门文章

  1. Spring中的applicationContext文件详解
  2. python字符的表示格式
  3. 36氪首发 | 掘金RPA百亿新蓝海,弘玑Cyclone获DCM、源码千万美元A轮融资
  4. [Codeforces Educational Round 71]Div. 2
  5. python zlib模块缺失报错:RuntimeError: Compression requires the (missing) zlib module
  6. 【转载】Visual Studio(VS) F12 查看DLL源代码
  7. learning scala extracors example
  8. linux下递归删除目录下所有exe文件---从删库到跑路篇
  9. HAVING 搜索条件在进行分组操作之后应用
  10. koa koa-static 静态资源中间件