介绍

Ecoder负责将事件转换为字节数组,并将该字节数组写出到OutputStream中

Encoder接口

public interface Encoder<E> extends ContextAware, LifeCycle {
    //日志打印时头部显示内容
    byte[] headerBytes();
    //日志转码
    byte[] encode(E event);
    //尾部内容,通常是结束之后调用
    byte[] footerBytes();
}

LayoutWrappingEncoder

在0.9.19版本的Logback之前,许多Appender都依赖Layout实例来控制日志输出的格式。由于存在大量基于布局接口的代码,因此我们需要一种encoder与layout进行互操作的方法。 LayoutWrappingEncoder弥合了encoder和layout之间的差距。它实现了编码器接口,并包装了一个布局,该布局委派了将事件转换为字符串的工作。

public class LayoutWrappingEncoder<E> extends EncoderBase<E> {

    protected Layout<E> layout;
    private Charset charset;
    Appender<?> parent;
    Boolean immediateFlush = null;
    @Override
    public byte[] headerBytes() {
        if (layout == null)
            return null;
        StringBuilder sb = new StringBuilder();
        appendIfNotNull(sb, layout.getFileHeader());
        appendIfNotNull(sb, layout.getPresentationHeader());
        if (sb.length() > 0) {
            sb.append(CoreConstants.LINE_SEPARATOR);
        }
        return convertToBytes(sb.toString());
    }
    @Override
    public byte[] footerBytes() {
        if (layout == null)
            return null;

        StringBuilder sb = new StringBuilder();
        appendIfNotNull(sb, layout.getPresentationFooter());
        appendIfNotNull(sb, layout.getFileFooter());
        return convertToBytes(sb.toString());
    }
    private byte[] convertToBytes(String s) {
        if (charset == null) {
            return s.getBytes();
        } else {
            return s.getBytes(charset);
        }
    }
    public byte[] encode(E event) {
        String txt = layout.doLayout(event);
        return convertToBytes(txt);
    }
    public void start() {
        if (immediateFlush != null) {
            if (parent instanceof OutputStreamAppender) {
                addWarn("Setting the \"immediateFlush\" property of the enclosing appender to " + immediateFlush);
                @SuppressWarnings("unchecked")
                OutputStreamAppender<E> parentOutputStreamAppender = (OutputStreamAppender<E>) parent;
                parentOutputStreamAppender.setImmediateFlush(immediateFlush);
            } else {
                addError("Could not set the \"immediateFlush\" property of the enclosing appender.");
            }
        }
        started = true;
    }
}

PatternLayoutEncoder

鉴于PatternLayout是最常用的布局,因此Logback通过PatternLayoutEncoder迎合了这种常见用例,PatternLayoutEncoder是LayoutWrappingEncoder的扩展,仅限于包装PatternLayout实例。

从0.9.19版开始,每当FileAppender或其子类之一配置有PatternLayout时,都必须改用PatternLayoutEncoder。

public class PatternLayoutEncoder extends PatternLayoutEncoderBase<ILoggingEvent> {
    @Override
    public void start() {
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.setContext(context);
        patternLayout.setPattern(getPattern());
        patternLayout.setOutputPatternAsHeader(outputPatternAsHeader);
        patternLayout.start();
        this.layout = patternLayout;
        super.start();
    }
}

encoder的默认class为PatternLayoutEncoder,可以省略

<appender name="STDOUT_HIGHLIGHT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>${highlightPattern}</pattern>
        <outputPatternAsHeader>true</outputPatternAsHeader>
        <immediateFlush>true</immediateFlush>
    </encoder>
</appender>

评论

博客
分类
标签
归档
关于