logback配置中的Encoder
# 介绍
Ecoder负责将事件转换为字节数组,并将该字节数组写出到OutputStream中
# Encoder接口
public interface Encoder<E> extends ContextAware, LifeCycle {
//日志打印时头部显示内容
byte[] headerBytes();
//日志转码
byte[] encode(E event);
//尾部内容,通常是结束之后调用
byte[] footerBytes();
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
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>
1
2
3
4
5
6
7
2
3
4
5
6
7
在 GitHub 编辑此页 (opens new window)
上次更新: 2024/02/25, 12:11:11