首页 > 其他分享 >SLF4J(W)报错No SLF4J providers were found.

SLF4J(W)报错No SLF4J providers were found.

时间:2024-12-28 16:19:59浏览次数:3  
标签:console providers SLF4J appender slf4j 报错 org log4j

问题

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

在开发项目时,如果我们的项目使用了SLF4J,或者引入了某开源项目时,他的项目中用了SLF4J,运行时会报出上述错误。
原因是,SLF4J本身不是一个日志实现库,而是一个日志库的抽象层,它必须依赖底层的日志库,SLF4J必须和其他日志库配合才能正常运行。一般来说,需要将抽象层(例如slf4j-api-xx.jar)+中间层(例如slf4j-log4j12)+实现层(例如log4j)这三层都配置好才能保证SLF4J正常运行。有的日志库也可以去掉中间层,例如slf4j-api和slf4j-simple就可以直接配合。

解决办法

抽象层+中间层+实现层的方式解决

引入下面的3个依赖,重新编译

<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>

在重新编译后,运行如果出现

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

要在resource路径下面添加一个配置文件log4j.properties,内容可以自定义

# Set root logger level to DEBUG and its only appender to console.
log4j.rootLogger=WARN, 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 = WARN
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

再次编译运行,即可看到日志输出

抽象层+实现层的方式解决

 <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>

转自:SLF4J 报错解决:No SLF4J providers were found

标签:console,providers,SLF4J,appender,slf4j,报错,org,log4j
From: https://www.cnblogs.com/lzmhc/p/18637594

相关文章