浅谈spring boot 集成 log4j 解决与logback冲突的问题
作者:思念是荒芜的梦
今天小编就为大家分享一篇浅谈spring boot 集成 log4j 解决与logback冲突的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
现在很流行springboot的开发,小编闲来无事也学了学,开发过程中遇见了log4j日志的一个小小问题,特此记载。
首先在pox.xml中引入对应的maven依赖:
<!-- 引入log4j--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>1.3.8.RELEASE</version> </dependency>
然后在src/resources/下新建log4j.properties文件(摘抄自网上):
log4j.rootLogger=info,error,CONSOLE,DEBUG log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.logger.info=info log4j.appender.info=org.apache.log4j.DailyRollingFileAppender log4j.appender.info.layout=org.apache.log4j.PatternLayout log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.info.datePattern='.'yyyy-MM-dd log4j.appender.info.Threshold = info log4j.appender.info.append=true log4j.appender.info.File=d://springboot3/logs/api_services_info.log log4j.logger.error=error log4j.appender.error=org.apache.log4j.DailyRollingFileAppender log4j.appender.error.layout=org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.error.datePattern='.'yyyy-MM-dd log4j.appender.error.Threshold = error log4j.appender.error.append=true log4j.appender.error.File=d://springboot3/logs/error/api_services_error.log log4j.logger.DEBUG=DEBUG log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd log4j.appender.DEBUG.Threshold = DEBUG log4j.appender.DEBUG.append=true log4j.appender.DEBUG.File=d://springboot3/logs/debug/api_services_debug.log
在application.yml中加入如下配置:
logging: config: src/main/resources/log4j.properties
至此,log4j的配置完毕,启动项目,发现会报错:
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/Users/tony/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/C:/Users/tony/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 12:02:26.963 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : [] 12:02:26.969 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/] 12:02:26.969 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/F:/work_intel/code/demo1/target/classes/] Logging system failed to initialize using configuration from 'src/main/resources/log4j.properties'
这是因为与logback的冲突并未解决,导致启动失败,根据上面的报错信息,找到logback-classic-1.2.3.jar和slf4j-log4j12-1.7.25.jar的相应位置删除,并在pom.xml中加入如下排除代码:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除默认的logback日志,使用log4j--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>
启动发现一切正常,至此,log4j的配置完毕。
以上这篇浅谈spring boot 集成 log4j 解决与logback冲突的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Java中 SLF4J和Logback和Log4j和Logging的区别与联系
- slf4j与jul、log4j1、log4j2、logback的集成原理
- jdk-logging log4j logback日志系统实现机制原理介绍
- jcl与jul log4j1 log4j2 logback日志系统机制及集成原理
- slf4j jcl jul log4j1 log4j2 logback各组件系统日志切换
- Logback与Log4j2日志框架性能对比与调优方式
- 项目为什么引入log4j而不是logback代码
- 如何将应用的log4j替换成logback详解
- 从log4j切换到logback后项目无法启动的问题及解决方法