`

读logback源码系列文章(三)——创建Logger

阅读更多
上一篇博客介绍了logback的StaticLoggerBinder类怎么初始化并创建LoggerContext,这篇博客准备接下来介绍一下,LoggerContext怎么创建日志框架真正的核心类Logger。为什么logback框架提供的Logger是树形结构的呢?这就是在LoggerContext里实现的

还是先上图,对LoggerContext和周边类的关系有一个整体上的认识



从图中可以看到,LoggerContext类除了ILoggerFactory接口之外,还实现了LifeCycle接口,并继承自ContextBase类

LoggerContext最核心的方法是getLogger(String name),这个方法放在最后介绍,先介绍一下其它字段和方法,这些字段和方法,大部分都是为了getLogger()方法服务的

首先需要澄清一点,虽然LoggerContext没有显式地采用单例模式,但实际上应用从来不会直接获取到LoggerContext类的实例,都是通过StaticLoggerBinder内的defaultLoggerContext字段来间接引用LoggerContext的实例,而StaticLoggerBinder本身是单例的,所以程序中用到的LoggerContext也始终是同一个实例,这点很重要

看看LoggerContext内的字段
final Logger root;
  private int size;
  private int noAppenderWarning = 0;
  final private List<LoggerContextListener> loggerContextListenerList = new ArrayList<LoggerContextListener>();

  // We want loggerCache to be synchronized so Hashtable is a good choice. In
  // practice, it performs a little faster than the map returned by
  // Collections.synchronizedMap at the cost of a very slightly higher memory
  // footprint.
  private Hashtable<String, Logger> loggerCache;

  private LoggerContextVO loggerContextRemoteView;
  private final TurboFilterList turboFilterList = new TurboFilterList();
  private boolean packagingDataEnabled = true;

  private int maxCallerDataDepth = ClassicConstants.DEFAULT_MAX_CALLEDER_DATA_DEPTH;

  boolean started = false;

  int resetCount = 0;

1、root是根Logger
2、size用来表示LoggerContext一共创建了几个Logger
3、loggerCache的名字起得有点误导性,实际上不止是一个cache,LoggerContext创建的所有Logger,都保存在这里,另外这里用的容器是Hashtable,我感到有点疑问,一般来说Hashtable是不推荐使用的,不知道最新版本的logback里,作者是否已经修改
4、loggerContextRemoteView是一个LoggerContext的VO对象,保存了LoggerContext的一些值,比如name、birthTime等,大家可以自己看看
5、turboFilterList保存所有的TurboFilter,TurboFilter顾名思义,是一种快速过滤器,对是否记录日志有一票通过和一票否决的权力,后面的博客在介绍Filter时,会专门介绍
6、resetCount是用来统计该LoggerContext调用过几次reset()方法
7、其它几个字段,我暂时不太清楚是干什么的

介绍完了字段,来看看LoggerContext的构造方法吧
public LoggerContext() {
    super();
    this.loggerCache = new Hashtable<String, Logger>();
    this.loggerContextRemoteView = new LoggerContextVO(this);
    this.root = new Logger(Logger.ROOT_LOGGER_NAME, null, this);
    this.root.setLevel(Level.DEBUG);
    loggerCache.put(Logger.ROOT_LOGGER_NAME, root);
    initEvaluatorMap();
    size = 1;
  }

这个方法基本上是只会调用一次的,在StaticLoggerBinder里
private LoggerContext defaultLoggerContext = new LoggerContext();

在构造LoggerContext的时候,对上面提到的字段进行了初始化,并创建了根Logger
/**
   * A new instance of LoggerContextRemoteView needs to be created each time the
   * name or propertyMap (including keys or values) changes.
   */
  private void syncRemoteView() {
    loggerContextRemoteView = new LoggerContextVO(this);
    for (Logger logger : loggerCache.values()) {
      logger.buildRemoteView();
    }
  }

上面的syncRemoteView()方法,是在LoggerContext的name字段和propertyMap发生变化的时候调用的,目的是创建新的LoggerContextVO,实际中不太会被调用到,不是核心方法
private void incSize() {
    size++;
  }

  int size() {
    return size;
  }

这2个方法都是和size字段相关的,很好理解

然后是一系列命名很奇怪的方法,只举一个最常用的例子
final FilterReply getTurboFilterChainDecision_0_3OrMore(final Marker marker,
      final Logger logger, final Level level, final String format,
      final Object[] params, final Throwable t) {
    if (turboFilterList.size() == 0) {
      return FilterReply.NEUTRAL;
    }
    return turboFilterList.getTurboFilterChainDecision(marker, logger, level,
        format, params, t);
  }

这个方法命名很烂,从命名上完全看不出这个方法的意图。该方法主要是在Logger要记录日志之前,用TurboFilter做一下过滤先,如果没有配置TurboFilter,就跳过过滤;否则的话,用TurboFilter做一下过滤。这部分内容涉及到fitler过滤,会在后面的博客里详细介绍

这个类中还有一系列侦听器方法,这篇博客先略过

接下来就是核心的getLogger()方法
public final Logger getLogger(final Class clazz) {
    return getLogger(clazz.getName());
  }

这个方法实际上调用的是下面这个方法
public final Logger getLogger(final String name) {

    if (name == null) {
      throw new IllegalArgumentException("name argument cannot be null");
    }

    // if we are asking for the root logger, then let us return it without
    // wasting time
    if (Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(name)) {
      return root;
    }

    int i = 0;
    Logger logger = root;

    // check if the desired logger exists, if it does, return it
    // without further ado.
    Logger childLogger = (Logger) loggerCache.get(name);
    // if we have the child, then let us return it without wasting time
    if (childLogger != null) {
      return childLogger;
    }

    // if the desired logger does not exist, them create all the loggers
    // in between as well (if they don't already exist)
    String childName;
    while (true) {
      int h = Logger.getSeparatorIndexOf(name, i);
      if (h == -1) {
        childName = name;
      } else {
        childName = name.substring(0, h);
      }
      // move i left of the last point
      i = h + 1;
      synchronized (logger) {
        childLogger = logger.getChildByName(childName);
        if (childLogger == null) {
          childLogger = logger.createChildByName(childName);
          loggerCache.put(childName, childLogger);
          incSize();
        }
      }
      logger = childLogger;
      if (h == -1) {
        return childLogger;
      }
    }
  }

上面的getLogger(String name)方法,就是创建Logger的关键,详细解读一下:

1、如果name是null,抛出异常,不过这个情况基本是不会发生的
if (name == null) {
      throw new IllegalArgumentException("name argument cannot be null");
    }

2、如果请求的是ROOT Logger,那么就直接返回root
// if we are asking for the root logger, then let us return it without
    // wasting time
    if (Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(name)) {
      return root;
    }

3、然后检查一下请求的Logger是否已经创建过了,如果已经创建过,就直接从loggerCache中返回
// check if the desired logger exists, if it does, return it
    // without further ado.
    Logger childLogger = (Logger) loggerCache.get(name);
    // if we have the child, then let us return it without wasting time
    if (childLogger != null) {
      return childLogger;
    }

4、如果还没创建过,那就开始逐层创建,比如请求的Logger的name是com.company.package.ClassName,那么一共会创建4个Logger,分别是Logger[com]、Logger[com.company]、Logger[com.company.package]、Logger[com.company.package.ClassName]
int i = 0;
    Logger logger = root;

// if the desired logger does not exist, them create all the loggers
    // in between as well (if they don't already exist)
    String childName;
    while (true) {
      int h = Logger.getSeparatorIndexOf(name, i);
      if (h == -1) {
        childName = name;
      } else {
        childName = name.substring(0, h);
      }
      // move i left of the last point
      i = h + 1;
      synchronized (logger) {
        childLogger = logger.getChildByName(childName);
        if (childLogger == null) {
          childLogger = logger.createChildByName(childName);
          loggerCache.put(childName, childLogger);
          incSize();
        }
      }
      logger = childLogger;
      if (h == -1) {
        return childLogger;
      }
    }

上面代码比较清晰,就是根据"."来解析name,然后创建Logger,每创建一个Logger,都放到loggerCache中,并且把size++

创建Child Logger是有点讲究的,除了创建Logger实例之外,还有维护父子关系,并且处理Level继承的问题,这个是在Logger类的createChildByName(String childName)方法里实现的
Logger createChildByName(final String childName) {
    int i_index = getSeparatorIndexOf(childName, this.name.length() + 1);
    if (i_index != -1) {
      throw new IllegalArgumentException("For logger [" + this.name
          + "] child name [" + childName
          + " passed as parameter, may not include '.' after index"
          + (this.name.length() + 1));
    }

    if (childrenList == null) {
      childrenList = new ArrayList<Logger>(DEFAULT_CHILD_ARRAY_SIZE);
    }
    Logger childLogger;
    childLogger = new Logger(childName, this, this.loggerContext);
    childrenList.add(childLogger);
    childLogger.effectiveLevelInt = this.effectiveLevelInt;
    return childLogger;
  }

首先检查一下name是否是合法的。然后创建childLogger
Logger(String name, Logger parent, LoggerContext loggerContext) {
    this.name = name;
    this.parent = parent;
    this.loggerContext = loggerContext;
    buildRemoteView();
    instanceCount++;
  }

在上面的this.parent = parent里,设置了父Logger

然后在childrenList.add(childLogger)里,将新创建的Logger加到子Logger列表里

最后把子Logger的生效级别设置为当前Logger的生效级别

总结一下创建Logger的完整流程:
1、如果请求ROOT logger,则直接返回root
2、如果请求的Logger已经存在,则直接返回
3、如果请求的Logger尚未创建,则从ROOT开始,级联创建所有Logger
4、每创建一个Logger,都要设置父子关系,继承生效级别(至于Appender是怎么继承的,在后面的博客里介绍)
5、每创建一个Logger,都将其放入loggerCache,并将size++

关于在每次调用LoggerFactory.getLogger(String name)时,logback是怎么返回Logger的,这篇博客就介绍完了。下一篇博客会介绍,当调用Logger.info(String msg)时,会发生的事情
  • 大小: 122.4 KB
分享到:
评论

相关推荐

    springboot、logback源码解读

    springboot、logback源码解读,对logback从初始化到,配置文件加载到日志打印,所有步骤的源码分析

    logback-1.1.2源码包

    包含所有的jar包和源代码 logback-core logback-classic logback-access pom.xml

    java版ss源码-logback-json-logger:logback-json-logger

    java版s源码logback-json-logger 这是一个用于应用程序记录器的 JSON 编码器。 它也使用 Mapped Diagnostic Contexts () 在每个日志消息中包含适当的上下文信息。 这些是可用于更改日志输出的属性(对于您的...

    扩展logback将日志输出到Kafka实例源码

    扩展logback将日志输出到Kafka实例源码,详情请参见博文:http://blog.csdn.net/l1028386804/article/details/79135948

    logback源代码

    rar包中包括:logback源代码,以及struts2的基础的页面跳转,logback虽然是开源的,但它依赖的jar包找全不也不容易,当然你也可以用Maven去下载是最好的喽。 你可以访问:...

    SpringBoot中自定义日志配置logback-spring.xml示例源码

    SpringBoot中自定义日志配置logback-spring.xml示例源码

    Logback所需的jar包

    免费获取Logback所需的jar包 打包合集 让你少走弯路 一.logback简介 1.logback: Logback是由log4j创始人设计的另一个开源日志组件。(好的日志记录方式可以提供我们足够多的定位错误的依据)。 2.主要有三个模块...

    logback+slf4j的JAR包和源码

    只要里面的logback-classic-1.1.7,logback-core-1.1.7,slf4j-api-1.7.21的JAR就可以打印出日志信息,而带有source表示对应的JAR包的源代码。可以要也可以不要

    Logback日志框架第三方jar包 免费获取,我不收积分

    logback-classic-1.2.3.jar logback-core- 1.2.3.jar slf4j-api-1.7.26.jar

    logback下载 日志文件jar包

    内置三个jar包 一个配置文件 logback.txt logback-classic-1.2.3.jar logback-core-1.2.3.jar slf4j-api-1.7.26.jar

    logback-1.0.1

    logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便...

    Slf4j+logback实现logback测试

    Slf4j+logback实现logback测试,Slf4j+logback实现logback测试

    LogBack 中文开发手册

    Logback 中文手册,清晰版. 简单地说,Logback 是一个 Java 领域的日志框架。它被认为是 Log4J 的继承人。 Logback 主要由三个模块组成: logback-core logback-classic logback-access

    spring boot logback demo

    spring boot logback demo 源码可见:https://github.com/kent124454731/spring-boot-logback。 logback的一些说明可参见文章:http://blog.csdn.net/u011794238/article/details/50770557 如有乱码问题可参见:...

    Android代码-logback

    Spring Security源码分析三:Spring Social 实现QQ社交登录 Spring Security源码分析四:Spring Social 实现微信社交登录 Spring Security源码分析五:Spring Security 实现短信登录 Spring Security源码分析六:...

    扩展logback将日志输出到Kafka实例扩展源码

    扩展logback将日志输出到Kafka实例扩展源码,详情参见博文:http://blog.csdn.net/l1028386804/article/details/79136841

    Logback类库含logback.xml配置文件

    该压缩包包含 logback类库所包含的jar包以及logback.xml配置文件(放到 src 目录),用于开发学习使用。

    logback.的jar包

    此zip包含logback-access-1.2.3和logback-classic-1.2.3和logback-core-1.2.3

    logback-core-1.2.10-API文档-中文版.zip

    赠送jar包:logback-core-1.2.10.jar; 赠送原API文档:logback-core-1.2.10-javadoc.jar; 赠送源代码:logback-core-1.2.10-sources.jar; 赠送Maven依赖信息文件:logback-core-1.2.10.pom; 包含翻译后的API文档...

    logback-0.9.18.zip

    logback-0.9.18.zip 官网下载

Global site tag (gtag.js) - Google Analytics