springboot log4j2不能打印框架错误日志的解决方案
使用springboot时老的框架使用的是log4j2,为了兼容不能够使用logback,按照网上的教程导入了log4j2但是不能够输入框架错误日志,例如bean命名重复,springmvc url映射重复,这些都是开发中所需要的友好提示
解决方案怎么出现的问题,就不详细描述了,原因是我引入的log42是散件不是基于springboot集成配置的,导致只能够输出基本信息,原理我会在 一个月后左右探究完毕补充。
下面我说说怎么正确用springboot输出springboot框架错误的
1. 排除原生logback日志,否则会冲突<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- spring boot 默认的日志框架是Logback,所以在引用log4j之前,需要先排除该包的依赖,再引入log4j2的依赖 --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>2. 引入集成springboot配置的log4j2
我出现的我问题就是这里,我引入的是原先ssm用的log4j2导致,没有正确注入,初始log4j失败了,所以选择最省事的法子,引入人家写好的log4jpom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
可以看看人家默认的pom.xml引入的log4j,就知道自己引用出错在哪里了
<dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.10.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.10.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-jul</artifactId> <version>2.10.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> <version>1.7.25</version> <scope>compile</scope> </dependency> </dependencies>
配置后的效果
如下图所示,我写了两个 @RequestMapping() value='listTree'的方法
@RequestMapping('listTree') @ResponseBody public JsonResult getAreaTree() {List<AreaModel> all = areaService.findAll();all.forEach(x -> x.setName(x.getAreaName()));return new JsonResult(all); } @RequestMapping('listTree') @ResponseBody public JsonResult getAreaTree2() {List<AreaModel> all = areaService.findAll();all.forEach(x -> x.setName(x.getAreaName()));return new JsonResult(all); }
正确打印错误日志
[reform]2019-01-09 09:31:47.570 [WARN]:Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’requestMappingHandlerMapping’ defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map ’areaController’ method public com.ld.model.json.JsonResult com.ld.action.area.AreaController.getAreaTree2()to {[/area/listTree]}: There is already ’areaController’ bean methodpublic com.ld.model.json.JsonResult com.ld.action.area.AreaController.getAreaTree() mapped. org.apache.commons.logging.impl.Jdk14Logger.log(Jdk14Logger.java:99)
springboot 2.0 log4j2日志打印问题日志不能正常打印,测试、线上环境未生成滚动日志
日志文件配置:控制台信息:解决办法:在pom 文件中
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions><!-- 排除自带的logback依赖 --><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-web</artifactId> <exclusions><!-- 排除自带的logback依赖 --><exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId></exclusion><exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId></exclusion> </exclusions></dependency>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。
相关文章: