日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder

發(fā)布時(shí)間:2024/4/17 82 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

以下為自己剛學(xué)習(xí)java遇到及排錯(cuò)過程

日志換成了 slf4j+logback,沒看到本地有日志文件生成,調(diào)試時(shí)發(fā)現(xiàn)有錯(cuò)誤提示(是在加載druid時(shí)出現(xiàn)的):

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.

 網(wǎng)上說沒有配置實(shí)際的日志實(shí)現(xiàn)包導(dǎo)致,但在pom.xml中是有配置的:

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.26</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</version></dependency><!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version><scope>test</scope> // 注意此處</dependency>

  找到出錯(cuò)的源碼:com\alibaba\druid\1.1.16\druid-1.1.16-sources.jar!\com\alibaba\druid\pool\DruidAbstractDataSource.java

public abstract class DruidAbstractDataSource extends WrapperAdapter implements DruidAbstractDataSourceMBean, DataSource, DataSourceProxy, Serializable {private static final long serialVersionUID = 1L;private final static Log LOG = LogFactory.getLog(DruidAbstractDataSource.class); // 此處... }

  com\alibaba\druid\1.1.16\druid-1.1.16-sources.jar!\com\alibaba\druid\support\logging\LogFactory.java

public class LogFactory {private static Constructor logConstructor;static {String logType= System.getProperty("druid.logType");if(logType != null){if(logType.equalsIgnoreCase("slf4j")){tryImplementation("org.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl");}else if(logType.equalsIgnoreCase("log4j")){tryImplementation("org.apache.log4j.Logger", "com.alibaba.druid.support.logging.Log4jImpl");}else if(logType.equalsIgnoreCase("log4j2")){tryImplementation("org.apache.logging.log4j.Logger", "com.alibaba.druid.support.logging.Log4j2Impl");}else if(logType.equalsIgnoreCase("commonsLog")){tryImplementation("org.apache.commons.logging.LogFactory","com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl");}else if(logType.equalsIgnoreCase("jdkLog")){tryImplementation("java.util.logging.Logger", "com.alibaba.druid.support.logging.Jdk14LoggingImpl");}}// 優(yōu)先選擇log4j,而非Apache Common Logging. 因?yàn)楹笳邿o法設(shè)置真實(shí)Log調(diào)用者的信息tryImplementation("org.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl"); // 此處tryImplementation("org.apache.log4j.Logger", "com.alibaba.druid.support.logging.Log4jImpl");tryImplementation("org.apache.logging.log4j.Logger", "com.alibaba.druid.support.logging.Log4j2Impl");tryImplementation("org.apache.commons.logging.LogFactory","com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl");tryImplementation("java.util.logging.Logger", "com.alibaba.druid.support.logging.Jdk14LoggingImpl");if (logConstructor == null) {try {logConstructor = NoLoggingImpl.class.getConstructor(String.class);} catch (Exception e) {throw new IllegalStateException(e.getMessage(), e);}}} }private static void tryImplementation(String testClassName, String implClassName) {if (logConstructor != null) {return;}try {Resources.classForName(testClassName);Class implClass = Resources.classForName(implClassName);logConstructor = implClass.getConstructor(new Class[] { String.class });Class<?> declareClass = logConstructor.getDeclaringClass();if (!Log.class.isAssignableFrom(declareClass)) {logConstructor = null;}try {if (null != logConstructor) {logConstructor.newInstance(LogFactory.class.getName()); // 此處}} catch (Throwable t) {logConstructor = null;}} catch (Throwable t) {// skip} }

com\alibaba\druid\1.1.16\druid-1.1.16-sources.jar!\com\alibaba\druid\support\logging\SLF4JImpl.java

public class SLF4JImpl implements Log {private static final String callerFQCN = SLF4JImpl.class.getName();private static final Logger testLogger = LoggerFactory.getLogger(SLF4JImpl.class); // 此處static {// if the logger is not a LocationAwareLogger instance, it can not get correct stack StackTraceElement// so ignore this implementation.if (!(testLogger instanceof LocationAwareLogger)) {throw new UnsupportedOperationException(testLogger.getClass() + " is not a suitable logger");}}... }

org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26-sources.jar!\org\slf4j\LoggerFactory.java

public static Logger getLogger(Class<?> clazz) {Logger logger = getLogger(clazz.getName()); // 此處if (DETECT_LOGGER_NAME_MISMATCH) {Class<?> autoComputedCallingClass = Util.getCallingClass();if (autoComputedCallingClass != null && nonMatchingClasses(clazz, autoComputedCallingClass)) {Util.report(String.format("Detected logger name mismatch. Given name: \"%s\"; computed name: \"%s\".", logger.getName(),autoComputedCallingClass.getName()));Util.report("See " + LOGGER_NAME_MISMATCH_URL + " for an explanation");}}return logger;}

public static Logger getLogger(String name) {ILoggerFactory iLoggerFactory = getILoggerFactory(); // 此處return iLoggerFactory.getLogger(name);}

public static ILoggerFactory getILoggerFactory() {if (INITIALIZATION_STATE == UNINITIALIZED) {synchronized (LoggerFactory.class) {if (INITIALIZATION_STATE == UNINITIALIZED) {INITIALIZATION_STATE = ONGOING_INITIALIZATION;performInitialization(); // 此處}}}switch (INITIALIZATION_STATE) {case SUCCESSFUL_INITIALIZATION:return StaticLoggerBinder.getSingleton().getLoggerFactory();case NOP_FALLBACK_INITIALIZATION:return NOP_FALLBACK_FACTORY;case FAILED_INITIALIZATION:throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG);case ONGOING_INITIALIZATION:// support re-entrant behavior.// See also http://jira.qos.ch/browse/SLF4J-97return SUBST_FACTORY;}throw new IllegalStateException("Unreachable code");}

private final static void performInitialization() {bind(); // 此處if (INITIALIZATION_STATE == SUCCESSFUL_INITIALIZATION) {versionSanityCheck();}}

private final static void bind() {try {...// skip check under android, see also// http://jira.qos.ch/browse/SLF4J-328if (!isAndroid()) {staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet(); // 此處reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);}...} catch () {...} }

static Set<URL> findPossibleStaticLoggerBinderPathSet() {// use Set instead of list in order to deal with bug #138// LinkedHashSet appropriate here because it preserves insertion order// during iterationSet<URL> staticLoggerBinderPathSet = new LinkedHashSet<URL>();try {ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader();Enumeration<URL> paths;if (loggerFactoryClassLoader == null) {paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH);} else {paths = loggerFactoryClassLoader.getResources(STATIC_LOGGER_BINDER_PATH);}while (paths.hasMoreElements()) {URL path = paths.nextElement();staticLoggerBinderPathSet.add(path);}} catch (IOException ioe) {...}return staticLoggerBinderPathSet; // 返回的size為零}

  找到生成目錄,缺少?logback-classic-1.2.3.jar

?最終發(fā)現(xiàn)是因?yàn)閜om.xml中影響(這段代碼直接從maven復(fù)制)

最終是此句影響,還是基礎(chǔ)不勞呀

?

轉(zhuǎn)載于:https://www.cnblogs.com/Wicher-lsl/p/11227609.html

總結(jié)

以上是生活随笔為你收集整理的SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。