Slf4j如何绑定到实现?它真的在编译时这样做吗? [英] How does slf4j bind to implementation? Does it really do so during compile time?
本文介绍了Slf4j如何绑定到实现?它真的在编译时这样做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在slf4j的文档中,它说绑定发生在编译时间:
"SLF4J不依赖于任何特殊的类加载器机械。事实上,每个SLF4J绑定在编译时都是硬连接的,以使用一个且只有一个特定的日志记录框架。例如,slf4j-log4j12-1.7.5.jar绑定在编译时绑定以使用log4j。在您的代码中,除了slf4j-api-1.7.5.jar之外,您只需将一个且只有一个您选择的绑定放到适当的类路径位置。不要在类路径上放置多个绑定。以下是大意的图解。"http://www.slf4j.org/manual.html
这是如何工作的?
推荐答案
这里是slf4j的源代码。 Slf4j将查找类路径中路径为"org/slf4j/impl/StaticLoggerBinder.class".的所有类如果有多个,JVM将只随机选取一个。有关更多详细信息,请查看此处:http://www.slf4j.org/codes.html#multiple_bindings
// We need to use the name of the StaticLoggerBinder class, but we can't
// reference
// the class itself.
private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";
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 iteration
Set<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) {
Util.report("Error getting resources from path", ioe);
}
return staticLoggerBinderPathSet;
}
这篇关于Slf4j如何绑定到实现?它真的在编译时这样做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文