logback

1、组件图

1.1 Appender

 

 Appender继承LifeCycle,FilterAttachable, ContextAware。

1.2 Layout

 Layout继承LifeCycle和ContextAware

1.3 配置文件

其结构如下

2、初始化

2.1 配置文件读取

是通过ContextInitializer.autoConfig实现的。首先读取属性logback.configurationFile,没有就使用logback-test.xml文件,如果没有就使用logback.groovy,如果没有使用logback.xml文件 ,如果没有使用加载器查找Configurator的实现类,如果没有就使用默认的BasicConfigurator。

public void autoConfig() throws JoranException {
        StatusListenerConfigHelper.installIfAsked(loggerContext);
        URL url = findURLOfDefaultConfigurationFile(true);
        if (url != null) {
            configureByResource(url);
        } else {
            Configurator c = EnvUtil.loadFromServiceLoader(Configurator.class);
            if (c != null) {
                try {
                    c.setContext(loggerContext);
                    c.configure(loggerContext);
                } catch (Exception e) {
                    throw new LogbackException(String.format("Failed to initialize Configurator: %s using ServiceLoader", c != null ? c.getClass()
                                    .getCanonicalName() : "null"), e);
                }
            } else {
                BasicConfigurator basicConfigurator = new BasicConfigurator();
                basicConfigurator.setContext(loggerContext);
                basicConfigurator.configure(loggerContext);
            }
        }
    }

public URL findURLOfDefaultConfigurationFile(boolean updateStatus) {
        ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this);
        URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus);
        if (url != null) {
            return url;
        }

        url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus);
        if (url != null) {
            return url;
        }

        url = getResource(GROOVY_AUTOCONFIG_FILE, myClassLoader, updateStatus);
        if (url != null) {
            return url;
        }

        return getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus);
    }

2.2 JoranConfigurator

配置文件的处理时序为

(1)创建SaxEventRecorder,构造SAXParser,解析配置文件 。

(2)构造SaxEventInterpreter,创建SimpleRuleStore,添加结点处理规则,同时添加隐式规则以及默认的嵌套组件规则。

(3)播放SaxEvent,处理xml节点,得到Model。

(4)创建DefaultProcessor,向DefaultProcessor添加Model与Handler的映射关系。

(5)DefaultProcessor处理model。

2.2.1 RuleStore和Action

用于添加ElementPath与元素处理Action之间的映射关系。Action抽象类定义处理元素开始,元素体及元素结束。其关系图为

2.2.2 ModelHandlerBase抽象类 

用于处理Model,包含的处理及后置处理

3 、日志级别

级别包括:TRACE, DEBUG, INFO ,WARN, ERROR

参考资料:

https://logback.qos.ch/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值