slf4j StaticLoggerBinder 静态绑定测试
所属分类 slf4j
浏览量 85
org.slf4j:slf4j-api:1.7.25
org.slf4j:slf4j-simple:1.7.25
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final Logger log = LoggerFactory.getLogger("demo");
log.info("hello,slf4j,info");
log.debug("hello,slf4j,debug");
去掉 依赖 slf4j-simple
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.
java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
static final NOPLoggerFactory NOP_FALLBACK_FACTORY = new NOPLoggerFactory();
项目里增加
org.slf4j.impl.MyLoggerFactory
org.slf4j.impl.StaticLoggerBinder.java:6
存在多个
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [file:/Users/dugang/dugang/my/work2024/demo/slf4jdemo/target/classes/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/dugang/.m2/repository/org/slf4j/slf4j-simple/1.7.25/slf4j-simple-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.MyLoggerFactory]
可注释掉 项目里的 StaticLoggerBinder
private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";
Set< URL> staticLoggerBinderPathSet = new LinkedHashSet();
try {
ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader();
Enumeration 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) {
Util.report("Error getting resources from path", ioe);
}
System.out.println(LoggerFactory.getILoggerFactory().getClass());
class org.slf4j.impl.MyLoggerFactory
class org.slf4j.impl.SimpleLoggerFactory
初始化状态
static final int UNINITIALIZED = 0;
static final int ONGOING_INITIALIZATION = 1;
static final int FAILED_INITIALIZATION = 2;
static final int SUCCESSFUL_INITIALIZATION = 3;
static final int NOP_FALLBACK_INITIALIZATION = 4;
static volatile int INITIALIZATION_STATE = UNINITIALIZED;
package org.slf4j.impl;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.helpers.NOPLogger;
public class MyLoggerFactory implements ILoggerFactory {
public Logger getLogger(String name){
return NOPLogger.NOP_LOGGER;
}
}
package org.slf4j.impl;
import org.slf4j.ILoggerFactory;
public class StaticLoggerBinder {
private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
private static final String loggerFactoryClassStr = MyLoggerFactory.class.getName();
private final ILoggerFactory loggerFactory = new MyLoggerFactory();
public static final StaticLoggerBinder getSingleton() {return SINGLETON;}
private StaticLoggerBinder() {}
public ILoggerFactory getLoggerFactory() {return this.loggerFactory;}
public String getLoggerFactoryClassStr() {return loggerFactoryClassStr;}
}
https://gitee.com/dyyx/work2024/tree/master/demo/slf4jdemo
上一篇
下一篇
常用的MQTT客户端与Broker
netty-socketio chat demo
jks pem cer pfx 证书
slf4j StaticLoggerBinder 静态绑定机制
经典粤语歌曲60首
Nginx 配置说明