springboot 使用logback啟動報警報錯的解決
springboot logback啟動報警報錯
報錯信息如下:
16:06:07,484 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 16:06:07,484 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 16:06:07,485 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Users/Administrator/Desktop/***/***/target/classes/logback.xml] 16:06:07,638 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set 16:06:07,646 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 16:06:07,653 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [consoleAppender]
先說解決方法:
把日志文件名稱改為-spring結(jié)尾就不報警了!!! logback-spring.xml
tips(springboot日志使用logback):
Spring Boot官方推薦優(yōu)先使用帶有-spring的文件名作為你的日志配置(如使用logback-spring.xml,而不是logback.xml),如果想自定義文件名,可以通過logging.config屬性指定自定義的名字:
logging.config=classpath:my-logging-config.xml
官方start包中已經(jīng)默認(rèn)集成了slf4j和logback不用導(dǎo)入任何日志依賴
推薦使用logback,性能優(yōu)于log4j
設(shè)置logback彩色日志
<property name="log.consolePattern" value="%highlight(%date{yyyy-MM-dd HH:mm:ss.SSS}) %boldYellow([%thread]) %highlight([%-5level]) %boldCyan([%replace(%caller{1}){'\t|Caller.{1}0|\r\n', ''}]) %msg%n"/>
<!-- 控制臺設(shè)置,主要是上方pattern修改就可以,其他地方可以用不同pattern -->
<appender name="CONSOLE_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.consolePattern}</pattern> <!-- 控制臺日志輸出格式 -->
<charset>utf8</charset>
</encoder>
</appender>
springboot多環(huán)境開發(fā)日志
<!-- 測試環(huán)境+開發(fā)環(huán)境. 多個使用逗號隔開 --> <springProfile name="test,dev"> <logger name="工程包路徑" level="info" /> </springProfile> <!-- 生產(chǎn)環(huán)境. --> <springProfile name="prod"> <logger name="工程包路徑" level="ERROR" /> </springProfile>
banner.txt
/*** * _ooOoo_ * o8888888o * 88" . "88 * (| -_- |) * O\ = /O * ___/`---'\____ * . ' \\| |// `. * / \\||| : |||// \ * / _||||| -:- |||||- \ * | | \\\ - /// | | * | \_| ''\---/'' | | * \ .-\__ `-` ___/-. / * ___`. .' /--.--\ `. . __ * ."" '< `.___\_<|>_/___.' >'"". * | | : `- \`.;`\ _ /`;.`/ - ` : | | * \ \ `-. \_ __\ /__ _/ .-` / / * ======`-.____`-.___\_____/___.-`____.-'====== * `=---=' * ............................................. * 佛曰:bug泛濫,我已癱瘓! */
springboot使用logback會遇到的坑
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/C:/Users/fyk/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.7/log4j-slf4j-impl-2.7.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory at org.springframework.util.Assert.instanceCheckFailed(Assert.java:389) at org.springframework.util.Assert.isInstanceOf(Assert.java:327) at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:274) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:98) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:230) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:209) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69) at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48) at org.springframework.boot.SpringApplication.run(SpringApplication.java:292) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) at com.light.SpringbootApplication.main(SpringbootApplication.java:32) ... 5 more
在spring boot中導(dǎo)入logback jar包會與spring-boot-starter-web沖突,應(yīng)該是springboot中已經(jīng)包含了這個包,
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
去掉這個導(dǎo)入就好了!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis Mapper XML文件-插入,更新,刪除詳解(insert, updat
這篇文章主要介紹了MyBatis的Mapper XML文件中用于插入、更新和刪除數(shù)據(jù)的語句,包括這些語句的屬性和子元素的使用方法2025-02-02
Java?C++題解leetcode902最大為N的數(shù)字組合數(shù)位DP
這篇文章主要為大家介紹了Java?C++題解leetcode902最大為N的數(shù)字組合數(shù)位DP,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Springboot下swagger-ui.html訪問不到的解決方案
這篇文章主要介紹了Springboot下swagger-ui.html訪問不到的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
springboot啟動報錯:application?startup?failed問題
這篇文章主要介紹了springboot啟動報錯:application?startup?failed問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
spring啟動后保證創(chuàng)建的對象不被垃圾回收器回收
最近看到一個問題是,spring在啟動后如何保證創(chuàng)建的對象不被垃圾回收器回收?。所以本文結(jié)合jvm的垃圾回收機制和spring中的源代碼做出自己的一點猜測。有需要的朋友們可以參考借鑒。2016-09-09

