|
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,51 +62,45 @@ public CompositeJsonEncoder() {
|
60 | 62 | protected abstract CompositeJsonFormatter<Event> createFormatter();
|
61 | 63 |
|
62 | 64 | @Override
|
63 |
| - public byte[] encode(Event event) { |
64 |
| - byte[] prefixBytes = doEncodeWrappedToBytes(prefix, event); |
65 |
| - byte[] suffixBytes = doEncodeWrappedToBytes(suffix, event); |
| 65 | + public void encode(Event event, OutputStream outputStream) throws IOException { |
| 66 | + if (!isStarted()) { |
| 67 | + throw new IllegalStateException("Encoder is not started."); |
| 68 | + } |
66 | 69 |
|
67 |
| - ByteArrayOutputStream outputStream = new ByteArrayOutputStream( |
68 |
| - minBufferSize |
69 |
| - + (prefixBytes == null ? 0 : prefixBytes.length) |
70 |
| - + (suffixBytes == null ? 0 : suffixBytes.length) |
71 |
| - + lineSeparatorBytes.length); |
72 |
| - try { |
73 |
| - if (prefixBytes != null) { |
74 |
| - outputStream.write(prefixBytes); |
75 |
| - } |
76 |
| - |
77 |
| - formatter.writeEventToOutputStream(event, outputStream); |
78 |
| - |
79 |
| - if (suffixBytes != null) { |
80 |
| - outputStream.write(suffixBytes); |
81 |
| - } |
82 |
| - |
83 |
| - outputStream.write(lineSeparatorBytes); |
84 |
| - |
| 70 | + encode(prefix, event, outputStream); |
| 71 | + formatter.writeEventToOutputStream(event, outputStream); |
| 72 | + encode(suffix, event, outputStream); |
| 73 | + |
| 74 | + outputStream.write(lineSeparatorBytes); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public byte[] encode(Event event) { |
| 79 | + try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream(getMinBufferSize())) { |
| 80 | + encode(event, outputStream); |
85 | 81 | return outputStream.toByteArray();
|
86 |
| - } catch (IOException e) { |
87 |
| - addWarn("Error encountered while encoding log event. " |
88 |
| - + "Event: " + event, e); |
89 |
| - return EMPTY_BYTES; |
90 |
| - } finally { |
91 |
| - try { |
92 |
| - outputStream.close(); |
93 |
| - } catch (IOException e) { |
94 |
| - throw new RuntimeException(e); |
95 |
| - } |
96 | 82 | }
|
| 83 | + catch (IOException e) { |
| 84 | + addWarn("Error encountered while encoding log event. Event: " + event, e); |
| 85 | + return EMPTY_BYTES; |
| 86 | + } |
97 | 87 | }
|
98 | 88 |
|
99 |
| - private byte[] doEncodeWrappedToBytes(Encoder<Event> wrapped, Event event) { |
100 |
| - if (wrapped != null) { |
101 |
| - return wrapped.encode(event); |
| 89 | + private void encode(Encoder<Event> encoder, Event event, OutputStream outputStream) throws IOException { |
| 90 | + if (encoder!=null) { |
| 91 | + byte[] data = encoder.encode(event); |
| 92 | + if (data!=null) { |
| 93 | + outputStream.write(data); |
| 94 | + } |
102 | 95 | }
|
103 |
| - return EMPTY_BYTES; |
104 | 96 | }
|
105 | 97 |
|
106 | 98 | @Override
|
107 | 99 | public void start() {
|
| 100 | + if (isStarted()) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
108 | 104 | super.start();
|
109 | 105 | formatter.setContext(getContext());
|
110 | 106 | formatter.start();
|
@@ -151,14 +147,16 @@ private void startWrapped(Encoder<Event> wrapped) {
|
151 | 147 |
|
152 | 148 | @Override
|
153 | 149 | public void stop() {
|
154 |
| - super.stop(); |
155 |
| - formatter.stop(); |
156 |
| - stopWrapped(prefix); |
157 |
| - stopWrapped(suffix); |
| 150 | + if (isStarted()) { |
| 151 | + super.stop(); |
| 152 | + formatter.stop(); |
| 153 | + stopWrapped(prefix); |
| 154 | + stopWrapped(suffix); |
| 155 | + } |
158 | 156 | }
|
159 | 157 |
|
160 | 158 | private void stopWrapped(Encoder<Event> wrapped) {
|
161 |
| - if (wrapped != null && !wrapped.isStarted()) { |
| 159 | + if (wrapped != null && wrapped.isStarted()) { |
162 | 160 | wrapped.stop();
|
163 | 161 | }
|
164 | 162 | }
|
|
0 commit comments