快捷搜索:

SLF4J 源码分析

SLF4J,即简单日志门面(Simple Logging Facade for Java),不是详细的日志办理规划,它只办事于各类各样的日志系统。按照官方的说法,SLF4J是一个用于日志系统的简单Facade,容许终极用户在支配其利用时应用其所盼望的日志System,

Paste_Image.png

Logger log = LoggerFactory.getLogger(getClass());

LoggerFactory 是一个final 类 也是一个日志工厂类 临盆 Logger 工具,看看getLogger 措施

public static Logger getLogger(Class clazz) {

return getLogger(clazz.getName());

}

public static Logger getLogger(String name) {

ILoggerFactory iLoggerFactory = getILoggerFactory();

return iLoggerFactory.getLogger(name);

}

public static ILoggerFactory getILoggerFactory() {

//要是是未初始化状态

if (INITIALIZATION_STATE == UNINITIALIZED) {

//初始化状态为 正在进行中

INITIALIZATION_STATE = ONGOING_INITIALIZATION;

//进入performIntialization 措施初始化!

performInitialization();

}

switch (INITIALIZATION_STATE) {

//要是初始化成功

case SUCCESSFUL_INITIALIZATION:

//获取详细的日志工厂工具

return StaticLoggerBinder.getSingleton().getLoggerFactory();

case NOP_FALLBACK_INITIALIZATION:

// 返回NOPLoggerFactory 日志工厂

return NOP_FALLBACK_FACTORY;

case FAILED_INITIALIZATION:

//初始化掉败 抛出非常

throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG);

case ONGOING_INITIALIZATION:

// 返回 SubstituteLoggerFactory 日志工厂

return TEMP_FACTORY;

}

throw new IllegalStateException("Unreachable code");

}

private final static void performInitialization() {

//绑定一个详细的日志实现

bind();

if (INITIALIZATION_STATE == SUCCESSFUL_INITIALIZATION) {

//反省日志的版本

versionSanityCheck();

}

}

private final static void bind() {

try {

//探求日志绑定实现类,假如找不到或者指定的措施不存在,都邑报错提示,实现类可能有多个,

//是以用Set聚拢

Set staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();

//申报打印

reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);

// 测试是否存在 getSingleton 措施

StaticLoggerBinder.getSingleton();

// 设置初始化状态,初始化成功

INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;

//申报打印实际的绑定类型

reportActualBinding(staticLoggerBinderPathSet);

emitSubstituteLoggerWarning();

//假如找不到指定的类

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

}

//假如找不到指定的措施 也便是 getSingleton 措施

} catch (java.lang.NoSuchMethodError nsme) {

String msg = nsme.getMessage();

if (msg != null && msg.indexOf("org.slf4j.impl.StaticLoggerBinder.getSingleton()") != -1) {

INITIALIZATION_STATE = FAILED_INITIALIZATION;

Util.report("slf4j-api 1.6.x (or later) is incompatible with this binding.");

Util.report("Your binding is version 1.5.5 or earlier.");

Util.report("Upgrade your binding to version 1.6.x.");

}

throw nsme;

} catch (Exception e) {

failedBinding(e);

throw new IllegalStateException("Unexpected initialization failure", e);

}

}

探求绑定实现类

private static Set findPossibleStaticLoggerBinderPathSet() {

Set staticLoggerBinderPathSet = new LinkedHashSet();

try {

//获取LoggerFactory的类加载器

ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader();

Enumeration paths;

//要是LoggerFactory的类加载器为空

if (loggerFactoryClassLoader == null) {

//假如获取不到类加载器则阐明是系统加载器,那么在系统路径下获取该资本文件

paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH);

} else {

//获取到了类加载器,则用该类加载器加载指定的资本文件

paths = loggerFactoryClassLoader

.getResources(STATIC_LOGGER_BINDER_PATH);

}

while (paths.hasMoreElements()) {

URL path = (URL) paths.nextElement();

//将地址加入到聚拢中去

staticLoggerBinderPathSet.add(path);

}

} catch (IOException ioe) {

Util.report("Error getting resources from path", ioe);

}

return staticLoggerBinderPathSet;

}

经由过程以上代码 找到了默认的 日志绑定实现类 StaticLoggerBinder,在经由过程该绑定类来获取Log4jLoggerFactory 详细的日志工厂工具

private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();

//单例

public static final StaticLoggerBinder getSingleton() {

return SINGLETON;

}

private StaticLoggerBinder() {

loggerFactory = new Log4jLoggerFactory();

try {

Level level = Level.TRACE;

} catch (NoSuchFieldError nsfe) {

Util

.report("This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version");

}

}

// 获取 Log4jLoggerFactory 类 工具

public ILoggerFactory getLoggerFactory() {

return loggerFactory;

}

在看看 Log4jLoggerFactory 类的 实现,Log4jLoggerFactory类 是对 ILoggerFactory接口的实现

ILoggerFactory 只定义了一个措施

public interface ILoggerFactory {

public Logger getLogger(String name);

}

public class Log4jLoggerFactory implements ILoggerFactory {

ConcurrentMap loggerMap;

public Log4jLoggerFactory() {

loggerMap = new ConcurrentHashMap();

}

public Logger getLogger(String name) {

//查看是否存在key 为 name 的 Logger工具

Logger slf4jLogger = loggerMap.get(name);

if (slf4jLogger != null) {

return slf4jLogger;

} else {

//这才是详细的日志实现

org.apache.log4j.Logger log4jLogger;

//获取log4jLogger 类工具

if(name.equalsIgnoreCase(Logger.ROOT_LOGGER_NAME))

log4jLogger = LogManager.getRootLogger();

else

log4jLogger = LogManager.getLogger(name);

//Log4jLoggerAdapter 是一个适配器类 用于适配Logger 接口

//由于 Log4jLoggerAdapter 承袭自 MarkerIgnoringBase 类

//而 MarkerIgnoringBase 类 又实现了 Logger 接口

// 所有的底层都是 log4jLogger 去实现的

Logger newInstance = new Log4jLoggerAdapter(log4jLogger);

Logger oldInstance = loggerMap.putIfAbsent(name, newInstance);

return oldInstance == null ? newInstance : oldInstance;

}

}

}

总结

Slf4j 主要用到了 JVM类加载机制 门面设计模式适配器设计模式

简单的说下它的道理,便是经由过程工厂类,供给一个用户的接口!用户可以经由过程这个外不雅接口,直接应用API实现日志的记录。而后面的详细实现由Slf4j来探求加载.探求的历程,便是经由过程类加载加载那个叫org/slf4j/impl/StaticLoggerBinder.class的文件,只要实现了这个文件的日志实现系统,都可以作为一种实现要领。假如找到很多种要领,那么就探求一种默认的要领

您可能还会对下面的文章感兴趣: