zoukankan      html  css  js  c++  java
  • SLF4J 报错解决:No SLF4J providers were found

    1.解决SLF4J报错

    我们在自己的项目中使用了SLF4J,或者引入了某开源项目时,他的项目中用了SLF4J,运行时会报如下的错误:

    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    

    检查自己的maven依赖,发现已经引入了slf4j-api-xx.jar了,这是为什么呢?其原因是,SLF4J本身不是一个日志实现库,而是一个日志库的抽象层,它必须依赖底层的日志库,SLF4J必须和其他日志库配合才能正常运行。一般来说,需要将抽象层(例如slf4j-api-xx.jar)+中间层(例如slf4j-log4j12)+实现层(例如log4j)这三层都配置好才能保证SLF4J正常运行。有的日志库也可以去掉中间层,例如slf4j-api和slf4j-simple就可以直接配合。

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

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

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

    代码测试后,发现SLF4J的报错就不存在了,如果出现新的报错如下:

    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已经配置好了,但是Log4j的配置还有问题。

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

    # 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
    

    再次运行后,输出正常了

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

    	<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

  • 相关阅读:
    salesforce零基础学习(九十七)Big Object
    关于linux 终端的小命令
    小知识点 之 JVM -XX:MaxGCPauseMillis 与 -XX:GCTimeRatio
    查看SAP HANA数据库最大连接数
    解决Failed to launch preferred application for category TerminalEmulator. Failed to execute child process /usr/lib/x86_64-linux-gnu/xfce4/exo-1/exo-helper-1 (No such file or directory)
    RxJava简析
    《UNIX编程艺术》学习1
    windows下的 长路径
    为什么说「动态类型一时爽,代码重构火葬场」?-强、弱,静态、动态 类型对比
    分布式系统概述(Hadoop与HBase的前生今世)
  • 原文地址:https://www.cnblogs.com/Fzeng/p/14387248.html
Copyright © 2011-2022 走看看