美文网首页
SLF4J: Failed to load class "org

SLF4J: Failed to load class "org

作者: tengwind | 来源:发表于2018-06-01 16:11 被阅读0次

    今天在搭建系统的时候,报了下面错误

    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.

    查到到这些错误实在slf4j-api的jar中的LoggerFactory类中 bind()方法抛出的

    private final static void bind() {
            try {
                Set<URL> staticLoggerBinderPathSet = null;
                // skip check under android, see also
                // http://jira.qos.ch/browse/SLF4J-328
                if (!isAndroid()) {
                    staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
                    reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);
                }
                // the next line does the binding
                StaticLoggerBinder.getSingleton();
                INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
                reportActualBinding(staticLoggerBinderPathSet);
                fixSubstituteLoggers();
                replayEvents();
                // release all resources in SUBST_FACTORY
                SUBST_FACTORY.clear();
            } catch (NoClassDefFoundError ncde) {
                String msg = ncde.getMessage();
                if (messageContainsOrgSlf4jImplStaticLoggerBinder(msg)) {
                    INITIALIZATION_STATE = NOP_FALLBACK_INITIALIZATION;
                    Util.report("Failed to load class \"org.slf4j.impl.StaticLoggerBinder\".");
                    Util.report("Defaulting to no-operation (NOP) logger implementation");
                    Util.report("See " + NO_STATICLOGGERBINDER_URL + " for further details.");
                } else {
                    failedBinding(ncde);
                    throw ncde;
                }
            }
    

    只贴出部分代码,上面代码报错的原因是StaticLoggerBinder.getSingleton();代码找不到StaticLoggerBinder这个类,这个类定义是在org.slf4j.impl.StaticLoggerBinder下面,但是slf4j-api包中没有实现这个类,而是交给终端用户指定,带来很大的灵活性。


    slf4j-api包

    从上面图也可以看出没有定义impl这个package,没有任何的实现类,所以要让这个不报错,还需要导入其他的jar包实现了StaticLoggerBinder这个类。从网上找了一下,有些jar包都实现了这个类。

    slf4j-log4j12.jar (for log4j logging),
    slf4j-jdk14.jar (for Java logging),
    slf4j-jcl.jar (if you’re using Commons logging)
    logback-classic.jar (for logback logging).

    只要引入相应的jar包即可解决这个问题。现在引入jdk的包slf4j-jdk14.jar

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
                <version>1.7.25</version>
            </dependency>
    
    jdk实现

    从上面的图中可以看到,包名和slf4j-api一样。

    相关文章

      网友评论

          本文标题:SLF4J: Failed to load class "org

          本文链接:https://www.haomeiwen.com/subject/silksftx.html