本文共 3606 字,大约阅读时间需要 12 分钟。
阅读源码之前,首先提几个问题
这段文字摘录自官网:In your code, in addition to slf4j-api-1.8.0-beta2.jar, you simply drop one and only one binding of your choice onto the appropriate class path location. Do not place more than one binding on your class path.
带着上面的两个问题看下源码
private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";
// org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar!/org/slf4j/LoggerFactory.classstatic SetfindPossibleStaticLoggerBinderPathSet() { LinkedHashSet staticLoggerBinderPathSet = new LinkedHashSet(); try { ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader(); Enumeration paths; // 通过ClassLoader或者loggerFactoryClassLoader来获取Resources 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 var4) { Util.report("Error getting resources from path", var4); } return staticLoggerBinderPathSet;}
// org/apache/logging/log4j/log4j-slf4j-impl/2.11.1/log4j-slf4j-impl-2.11.1.jar!/org/slf4j/impl/StaticLoggerBinder.class// 这里的包名定义为org.slf4j.impl,使得上述第2步的Class Loader可以加载到这个类package org.slf4j.impl;public final class StaticLoggerBinder implements LoggerFactoryBinder {// log4j init...}
// ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class// 这里的包名定义为org.slf4j.impl,使得上述第2步的Class Loader可以加载到这个类package org.slf4j.impl;public class StaticLoggerBinder implements LoggerFactoryBinder {// logback init...}
// org/slf4j/slf4j-jcl/1.7.25/slf4j-jcl-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class// 这里的包名定义为org.slf4j.impl,使得上述第2步的Class Loader可以加载到这个类package org.slf4j.impl;public class StaticLoggerBinder implements LoggerFactoryBinder {// jcl init...}
private static void reportMultipleBindingAmbiguity(SetbinderPathSet) { if (isAmbiguousStaticLoggerBinderPathSet(binderPathSet)) { Util.report("Class path contains multiple SLF4J bindings."); Iterator i$ = binderPathSet.iterator(); while(i$.hasNext()) { URL path = (URL)i$.next(); Util.report("Found binding in [" + path + "]"); } Util.report("See http://www.slf4j.org/codes.html#multiple_bindings for an explanation."); }}
代码中使用SLF4J来记录日志,可以任意切换底层日志框架而不需要修改代码,只需要更新依赖以及日志配置文件即可。
转载地址:http://vzgbi.baihongyu.com/