博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SLF4J源码解析(一)
阅读量:4031 次
发布时间:2019-05-24

本文共 3606 字,大约阅读时间需要 12 分钟。

提出问题

阅读源码之前,首先提几个问题

  • SLF4J是如何整合不同的日志框架的
  • Class Path中为什么只能有且仅有一种日志框架的binding

这段文字摘录自官网: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.

源码版本

  • org.slf4j:slf4j-api:1.7.25
  • org.apache.logging.log4j:log4j-slf4j-impl:2.11.1
  • ch.qos.logback:logback-classic:1.2.3
  • org.slf4j:slf4j-jcl:1.7.25

源码解析

带着上面的两个问题看下源码

  1. bind()方法中通过调用findPossibleStaticLoggerBinderPathSet()方法来查找日志框架的绑定

  1. findPossibleStaticLoggerBinderPathSet()方法中通过ClassLoader或者loggerFactoryClassLoader来获取名为"STATIC_LOGGER_BINDER_PATH"的Resources "STATIC_LOGGER_BINDER_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 Set
findPossibleStaticLoggerBinderPathSet() { 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;}
  1. 这里拿整合Log4j举例,"org/slf4j/impl/StaticLoggerBinder.class"就在log4j的log4j-slf4j-impl库中,这个类负责初始化Log4j相关的类。StaticLoggerBinder实现了LoggerFactoryBinder接口。
// 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...}
  1. 其他日志框架也定义了StaticLoggerBinder类,且实现了LoggerFactoryBinder接口。 例如: logback,jcl
// 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...}

问题答案

  • SLF4J是如何整合不同的日志框架的 SLF4J通过加载各个底层日志框架桥接库的org/slf4j/impl/StaticLoggerBinder.class来加载初始化对应的日志框架。
  • Class Path中为什么只能有且仅有一种日志框架的binding slf4j-api中的LoggerFactory中,在上述第2步返回staticLoggerBinderPathSet后,会立马调用 reportMultipleBindingAmbiguity(staticLoggerBinderPathSet)方法来检测是否有多个binding。如果有多个binding,就输出错误信息"Class path contains multiple SLF4J bindings."
private static void reportMultipleBindingAmbiguity(Set
binderPathSet) { 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/

你可能感兴趣的文章
Flex动态获取flash资源库文件
查看>>
flex中设置Label标签文字的自动换行
查看>>
Flex 中的元数据标签
查看>>
flex4 中创建自定义弹出窗口
查看>>
01Java基础语法-11. 数据类型之间的转换
查看>>
01Java基础语法-13. if分支语句的灵活使用
查看>>
01Java基础语法-15.for循环结构
查看>>
01Java基础语法-16. while循环结构
查看>>
01Java基础语法-17. do..while循环结构
查看>>
01Java基础语法-18. 各种循环语句的区别和应用场景
查看>>
01Java基础语法-19. 循环跳转控制语句
查看>>
Django框架全面讲解 -- Form
查看>>
socket,accept函数解析
查看>>
今日互联网关注(写在清明节后):每天都有值得关注的大变化
查看>>
”舍得“大法:把自己的优点当缺点倒出去
查看>>
[今日关注]鼓吹“互联网泡沫,到底为了什么”
查看>>
[互联网学习]如何提高网站的GooglePR值
查看>>
[关注大学生]求职不可不知——怎样的大学生不受欢迎
查看>>
[关注大学生]读“贫困大学生的自白”
查看>>
[互联网关注]李开复教大学生回答如何学好编程
查看>>