|
15 | 15 |
|
16 | 16 | import java.io.ByteArrayOutputStream;
|
17 | 17 | import java.io.IOException;
|
| 18 | +import java.io.OutputStream; |
18 | 19 | import java.nio.charset.Charset;
|
19 | 20 |
|
20 | 21 | import net.logstash.logback.composite.CompositeJsonFormatter;
|
21 | 22 | import net.logstash.logback.composite.JsonProviders;
|
22 | 23 | import net.logstash.logback.decorate.JsonFactoryDecorator;
|
23 | 24 | import net.logstash.logback.decorate.JsonGeneratorDecorator;
|
| 25 | + |
24 | 26 | import ch.qos.logback.core.encoder.Encoder;
|
25 | 27 | import ch.qos.logback.core.encoder.EncoderBase;
|
26 | 28 | import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
|
27 | 29 | import ch.qos.logback.core.pattern.PatternLayoutBase;
|
28 | 30 | import ch.qos.logback.core.spi.DeferredProcessingAware;
|
29 | 31 |
|
30 | 32 | public abstract class CompositeJsonEncoder<Event extends DeferredProcessingAware>
|
31 |
| - extends EncoderBase<Event> { |
| 33 | + extends EncoderBase<Event> implements StreamingEncoder<Event> { |
32 | 34 |
|
33 | 35 | private static final byte[] EMPTY_BYTES = new byte[0];
|
34 | 36 |
|
@@ -60,54 +62,48 @@ public CompositeJsonEncoder() {
|
60 | 62 | protected abstract CompositeJsonFormatter<Event> createFormatter();
|
61 | 63 |
|
62 | 64 | @Override
|
63 |
| - public byte[] encode(Event event) { |
| 65 | + public void encode(Event event, OutputStream outputStream) throws IOException { |
64 | 66 | if (!isStarted()) {
|
65 | 67 | throw new IllegalStateException("Encoder is not started");
|
66 | 68 | }
|
67 |
| - byte[] prefixBytes = doEncodeWrappedToBytes(prefix, event); |
68 |
| - byte[] suffixBytes = doEncodeWrappedToBytes(suffix, event); |
| 69 | + if (!isStarted()) { |
| 70 | + throw new IllegalStateException("Encoder is not started."); |
| 71 | + } |
69 | 72 |
|
70 |
| - ByteArrayOutputStream outputStream = new ByteArrayOutputStream( |
71 |
| - minBufferSize |
72 |
| - + (prefixBytes == null ? 0 : prefixBytes.length) |
73 |
| - + (suffixBytes == null ? 0 : suffixBytes.length) |
74 |
| - + lineSeparatorBytes.length); |
75 |
| - try { |
76 |
| - if (prefixBytes != null) { |
77 |
| - outputStream.write(prefixBytes); |
78 |
| - } |
79 |
| - |
80 |
| - formatter.writeEventToOutputStream(event, outputStream); |
81 |
| - |
82 |
| - if (suffixBytes != null) { |
83 |
| - outputStream.write(suffixBytes); |
84 |
| - } |
85 |
| - |
86 |
| - outputStream.write(lineSeparatorBytes); |
87 |
| - |
| 73 | + encode(prefix, event, outputStream); |
| 74 | + formatter.writeEventToOutputStream(event, outputStream); |
| 75 | + encode(suffix, event, outputStream); |
| 76 | + |
| 77 | + outputStream.write(lineSeparatorBytes); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public byte[] encode(Event event) { |
| 82 | + try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream(getMinBufferSize())) { |
| 83 | + encode(event, outputStream); |
88 | 84 | return outputStream.toByteArray();
|
89 |
| - } catch (IOException e) { |
90 |
| - addWarn("Error encountered while encoding log event. " |
91 |
| - + "Event: " + event, e); |
92 |
| - return EMPTY_BYTES; |
93 |
| - } finally { |
94 |
| - try { |
95 |
| - outputStream.close(); |
96 |
| - } catch (IOException e) { |
97 |
| - throw new RuntimeException(e); |
98 |
| - } |
99 | 85 | }
|
| 86 | + catch (IOException e) { |
| 87 | + addWarn("Error encountered while encoding log event. Event: " + event, e); |
| 88 | + return EMPTY_BYTES; |
| 89 | + } |
100 | 90 | }
|
101 | 91 |
|
102 |
| - private byte[] doEncodeWrappedToBytes(Encoder<Event> wrapped, Event event) { |
103 |
| - if (wrapped != null) { |
104 |
| - return wrapped.encode(event); |
| 92 | + private void encode(Encoder<Event> encoder, Event event, OutputStream outputStream) throws IOException { |
| 93 | + if (encoder!=null) { |
| 94 | + byte[] data = encoder.encode(event); |
| 95 | + if (data!=null) { |
| 96 | + outputStream.write(data); |
| 97 | + } |
105 | 98 | }
|
106 |
| - return EMPTY_BYTES; |
107 | 99 | }
|
108 | 100 |
|
109 | 101 | @Override
|
110 | 102 | public void start() {
|
| 103 | + if (isStarted()) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
111 | 107 | super.start();
|
112 | 108 | formatter.setContext(getContext());
|
113 | 109 | formatter.start();
|
@@ -154,14 +150,16 @@ private void startWrapped(Encoder<Event> wrapped) {
|
154 | 150 |
|
155 | 151 | @Override
|
156 | 152 | public void stop() {
|
157 |
| - super.stop(); |
158 |
| - formatter.stop(); |
159 |
| - stopWrapped(prefix); |
160 |
| - stopWrapped(suffix); |
| 153 | + if (isStarted()) { |
| 154 | + super.stop(); |
| 155 | + formatter.stop(); |
| 156 | + stopWrapped(prefix); |
| 157 | + stopWrapped(suffix); |
| 158 | + } |
161 | 159 | }
|
162 | 160 |
|
163 | 161 | private void stopWrapped(Encoder<Event> wrapped) {
|
164 |
| - if (wrapped != null && !wrapped.isStarted()) { |
| 162 | + if (wrapped != null && wrapped.isStarted()) { |
165 | 163 | wrapped.stop();
|
166 | 164 | }
|
167 | 165 | }
|
|
0 commit comments