亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SLF4J報(bào)錯(cuò)解決:No SLF4J providers were found的問題

 更新時(shí)間:2023年06月19日 14:21:14   作者:javafg  
這篇文章主要介紹了SLF4J報(bào)錯(cuò)解決:No SLF4J providers were found的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1.解決SLF4J報(bào)錯(cuò)

我們在自己的項(xiàng)目中使用了SLF4J,或者引入了某開源項(xiàng)目時(shí),他的項(xiàng)目中用了SLF4J,

運(yùn)行時(shí)會報(bào)如下的錯(cuò)誤:

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.

檢查自己的maven依賴,發(fā)現(xiàn)已經(jīng)引入了slf4j-api-xx.jar了,這是為什么呢?

其原因是,SLF4J本身不是一個(gè)日志實(shí)現(xiàn)庫,而是一個(gè)日志庫的抽象層,它必須依賴底層的日志庫,SLF4J必須和其他日志庫配合才能正常運(yùn)行。

一般來說,需要將抽象層(例如slf4j-api-xx.jar)+中間層(例如slf4j-log4j12)+實(shí)現(xiàn)層(例如log4j)這三層都配置好才能保證SLF4J正常運(yùn)行。

有的日志庫也可以去掉中間層,例如slf4j-api和slf4j-simple就可以直接配合。

2.抽象層+中間層+實(shí)現(xiàn)層的方式解決

引入三個(gè)下面三個(gè)依賴,重新編譯

<dependency>
? <groupId>org.slf4j</groupId>
? <artifactId>slf4j-api</artifactId>
? <version>1.8.0-beta4</version>
</dependency>
<dependency>
? <groupId>org.slf4j</groupId>
? <artifactId>slf4j-log4j12</artifactId>
? <version>1.8.0-beta4</version>
</dependency>
<dependency>
? <groupId>log4j</groupId>
? <artifactId>log4j</artifactId>
? <version>1.2.17</version>
</dependency>

代碼測試后,發(fā)現(xiàn)SLF4J的報(bào)錯(cuò)不存在了,但是出現(xiàn)了新的報(bào)錯(cuò)如下:

log4j:WARN No appenders could be found for logger.
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

這說明SLF4J已經(jīng)配置好了,但是Log4j的配置還有問題。

這需要在resource路徑下面添加一個(gè)配置文件log4j.properties,內(nèi)容如下(內(nèi)容可以自定義):

# Set root logger level to DEBUG and its only appender to console.
log4j.rootLogger=DEBUG, console
# console is set to be a ConsoleAppender.
log4j.appender.console=org.apache.log4j.ConsoleAppender
# console uses PatternLayout.
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold = DEBUG
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

再次運(yùn)行后,輸出正常了

3.抽象層+實(shí)現(xiàn)層的方式解決

引入下面的依賴,重新編譯后就正常了,注意假如你從maven庫里復(fù)制來的有 test,

需要改成 compile或者就不加這個(gè)scope,不加的時(shí)候默認(rèn)是compile,才能正常使用,

test表示只有在測試環(huán)境下,才可以使用,而springboot是運(yùn)行在main方法中,不屬于測試環(huán)境,所以這個(gè)包就相當(dāng)于沒有加入依賴,也就沒有被加載到。

改成compile則表示編譯的時(shí)候就會加載此jar包

?? ?<dependency>
? ? ? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? ? ? <artifactId>slf4j-api</artifactId>
? ? ? ? ? ? <version>1.8.0-beta4</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? ? ? <artifactId>slf4j-simple</artifactId>
? ? ? ? ? ? <version>1.8.0-beta4</version>
? ? ? ? </dependency>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論