zno2

java 定义 appender

appender 是必须用java实现的,可以在xml中传入一些参数,也可以实现自己的appender

仅仅在xml中声明appender 是不起作用的,必须通过 <logger>元素 或者 <root>元素 指明使用哪个appender

这个元素是 appender-ref 注意这是一个元素,不是任何其他元素的属性

可以参考源码

http://logback.qos.ch/xref/ch/qos/logback/core/joran/action/AppenderRefAction.html

/**
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 */
package ch.qos.logback.classic.joran;

import ch.qos.logback.classic.joran.action.*;
import ch.qos.logback.classic.sift.SiftAction;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.PlatformInfo;
import ch.qos.logback.classic.util.DefaultNestedComponentRules;
import ch.qos.logback.core.joran.JoranConfiguratorBase;
import ch.qos.logback.core.joran.action.AppenderRefAction;
import ch.qos.logback.core.joran.action.IncludeAction;
import ch.qos.logback.core.joran.action.NOPAction;
import ch.qos.logback.core.joran.conditional.ElseAction;
import ch.qos.logback.core.joran.conditional.IfAction;
import ch.qos.logback.core.joran.conditional.ThenAction;
import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
import ch.qos.logback.core.joran.spi.ElementSelector;
import ch.qos.logback.core.joran.spi.RuleStore;

/**
 * JoranConfigurator class adds rules specific to logback-classic.
 *
 * @author Ceki G&uuml;lc&uuml;
 */
public class JoranConfigurator extends JoranConfiguratorBase {

    @Override
    public void addInstanceRules(RuleStore rs) {
        // parent rules already added
        super.addInstanceRules(rs);

        rs.addRule(new ElementSelector("configuration"), new ConfigurationAction());

        rs.addRule(new ElementSelector("configuration/contextName"), new ContextNameAction());
        rs.addRule(new ElementSelector("configuration/contextListener"), new LoggerContextListenerAction());
        rs.addRule(new ElementSelector("configuration/insertFromJNDI"), new InsertFromJNDIAction());
        rs.addRule(new ElementSelector("configuration/evaluator"), new EvaluatorAction());

        rs.addRule(new ElementSelector("configuration/appender/sift"), new SiftAction());
        rs.addRule(new ElementSelector("configuration/appender/sift/*"), new NOPAction());

        rs.addRule(new ElementSelector("configuration/logger"), new LoggerAction());
        rs.addRule(new ElementSelector("configuration/logger/level"), new LevelAction());

        rs.addRule(new ElementSelector("configuration/root"), new RootLoggerAction());
        rs.addRule(new ElementSelector("configuration/root/level"), new LevelAction());
        rs.addRule(new ElementSelector("configuration/logger/appender-ref"), new AppenderRefAction<ILoggingEvent>());
        rs.addRule(new ElementSelector("configuration/root/appender-ref"), new AppenderRefAction<ILoggingEvent>());

        // add if-then-else support
        rs.addRule(new ElementSelector("*/if"), new IfAction());
        rs.addRule(new ElementSelector("*/if/then"), new ThenAction());
        rs.addRule(new ElementSelector("*/if/then/*"), new NOPAction());
        rs.addRule(new ElementSelector("*/if/else"), new ElseAction());
        rs.addRule(new ElementSelector("*/if/else/*"), new NOPAction());

        // add jmxConfigurator only if we have JMX available.
        // If running under JDK 1.4 (retrotranslateed logback) then we
        // might not have JMX.
        if (PlatformInfo.hasJMXObjectName()) {
            rs.addRule(new ElementSelector("configuration/jmxConfigurator"), new JMXConfiguratorAction());
        }
        rs.addRule(new ElementSelector("configuration/include"), new IncludeAction());

        rs.addRule(new ElementSelector("configuration/consolePlugin"), new ConsolePluginAction());

        rs.addRule(new ElementSelector("configuration/receiver"), new ReceiverAction());

    }

    @Override
    protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegistry registry) {
        DefaultNestedComponentRules.addDefaultNestedComponentRegistryRules(registry);
    }

}
JoranConfigurator

 

    <logger name="cn.zno.p1" level="DEBUG">
        <appender-ref ref="STDOUT1"></appender-ref>
    </logger>

 

不存在下面这种使用方式,appender-ref 不是属性

<logger name="cn.zno.p2" level="DEBUG" appender-ref="STDOUT2"></logger>

 

如果一个logger 不指定 appender-ref ,则对上下文中所有匹配level的appender生效

如果一个logger 指定了appender-ref ,则对指定的appender-ref 生效(可以指定多个)

logger可以通过name 对package 过滤

<configuration>

    <appender name="STDOUT1" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>1 %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT2" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>2 %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="cn.zno.p1" level="DEBUG">
        <appender-ref ref="STDOUT1"></appender-ref>
    </logger>
    <logger name="cn.zno.p2" level="DEBUG">
        <appender-ref ref="STDOUT2"></appender-ref>
    </logger>

</configuration>

 

一个日志信息会匹配所有满足条件的logger

 

1. 在一个日志输出的兄弟链(都匹配到了它)中,只要有一个有 additivity="false" ,则不会向root 层传递

2. 在兄弟链中匹配到了相同的appender,则只执行第一个

3. root 中有相同的appender-ref 只执行第一个

posted on 2023-06-01 16:44  zno2  阅读(82)  评论(0编辑  收藏  举报

导航