1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core;
15
16 import static ch.qos.logback.core.CoreConstants.CODES_URL;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.util.concurrent.locks.ReentrantLock;
21
22 import ch.qos.logback.core.encoder.Encoder;
23 import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
24 import ch.qos.logback.core.rolling.LengthCounter;
25 import ch.qos.logback.core.spi.DeferredProcessingAware;
26 import ch.qos.logback.core.status.ErrorStatus;
27
28
29
30
31
32
33
34
35
36
37 public class OutputStreamAppender<E> extends UnsynchronizedAppenderBase<E> {
38
39
40
41
42
43 protected Encoder<E> encoder;
44
45
46
47
48 protected final ReentrantLock streamWriteLock = new ReentrantLock(false);
49
50
51
52
53 private OutputStream outputStream;
54
55 boolean immediateFlush = true;
56
57 boolean statefulEncoder = false;
58
59
60
61
62
63
64 public OutputStream getOutputStream() {
65 return outputStream;
66 }
67
68
69
70
71
72 public void start() {
73 int errors = 0;
74 if (this.encoder == null) {
75 addError("No encoder set for the appender named \"" + name + "\".");
76 addWarn("Encoder has not been set. Cannot invoke its init method.");
77 errors++;
78 }
79
80 if (this.outputStream == null) {
81 addError("No output stream set for the appender named \"" + name + "\".");
82 errors++;
83 }
84
85
86
87 if (errors == 0) {
88 this.statefulEncoder = encoder.isStateful();
89
90
91 streamWriteLock.lock();
92 try {
93 super.start();
94 encoderInit();
95 } finally {
96 streamWriteLock.unlock();
97 }
98 }
99 }
100
101 public void setLayout(Layout<E> layout) {
102 addWarn("This appender no longer admits a layout as a sub-component, set an encoder instead.");
103 addWarn("To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.");
104 addWarn("See also " + CODES_URL + "#layoutInsteadOfEncoder for details");
105 LayoutWrappingEncoder<E> lwe = new LayoutWrappingEncoder<E>();
106 lwe.setLayout(layout);
107 lwe.setContext(context);
108 this.encoder = lwe;
109 }
110
111 @Override
112 protected void append(E eventObject) {
113 if (!isStarted()) {
114 return;
115 }
116
117 subAppend(eventObject);
118 }
119
120
121
122
123
124
125
126
127
128
129 public void stop() {
130 if(!isStarted())
131 return;
132
133 streamWriteLock.lock();
134 try {
135 closeOutputStream();
136 super.stop();
137 stopEncoder();
138 } finally {
139 streamWriteLock.unlock();
140 }
141 }
142
143
144
145
146
147
148 private void stopEncoder() {
149 if (encoder != null && encoder.isStarted()) {
150 encoder.stop();
151 }
152 }
153
154
155
156
157 protected void closeOutputStream() {
158 if (this.outputStream != null) {
159 try {
160
161 encoderClose();
162 this.outputStream.close();
163 this.outputStream = null;
164 } catch (IOException e) {
165 addStatus(new ErrorStatus("Could not close output stream for OutputStreamAppender.", this, e));
166 }
167 }
168 }
169
170
171
172
173
174
175 void encoderClose() {
176 if (encoder != null && this.outputStream != null) {
177 try {
178 byte[] footer = encoder.footerBytes();
179 writeBytes(footer);
180 } catch (IOException ioe) {
181 this.started = false;
182 addStatus(new ErrorStatus("Failed to write footer for appender named [" + name + "].", this, ioe));
183 }
184 }
185 }
186
187
188
189
190
191
192
193
194
195
196
197 public void setOutputStream(OutputStream outputStream) {
198 streamWriteLock.lock();
199 try {
200
201 closeOutputStream();
202 this.outputStream = outputStream;
203
204
205
206
207
208 if(isStarted()) {
209 encoderInit();
210 }
211 } finally {
212 streamWriteLock.unlock();
213 }
214 }
215
216
217
218
219
220
221 void encoderInit() {
222 if (encoder != null && this.outputStream != null) {
223 try {
224 byte[] header = encoder.headerBytes();
225 writeBytes(header);
226 } catch (IOException ioe) {
227 this.started = false;
228 addError("Failed to initialize encoder for appender named [" + name + "].", ioe);
229 }
230 }
231 }
232
233 protected void writeOut(E event) throws IOException {
234 if (statefulEncoder) {
235 writeOutStateful(event);
236 return;
237 }
238 writeBytes(this.encoder.encode(event));
239 }
240
241 private void writeOutStateful(E event) throws IOException {
242 streamWriteLock.lock();
243 try {
244
245
246 if (!isStarted()) {
247 return;
248 }
249 writeBytes(this.encoder.encode(event));
250 } finally {
251 streamWriteLock.unlock();
252 }
253 }
254
255 private void writeBytes(byte[] byteArray) throws IOException {
256 if (byteArray == null || byteArray.length == 0)
257 return;
258
259 streamWriteLock.lock();
260 try {
261
262 if(isStarted()) {
263 writeByteArrayToOutputStreamWithPossibleFlush(byteArray);
264 updateByteCount(byteArray);
265 }
266 } finally {
267 streamWriteLock.unlock();
268 }
269 }
270
271 protected void updateByteCount(byte[] byteArray) {
272 }
273
274
275
276
277
278
279 protected final void writeByteArrayToOutputStreamWithPossibleFlush(byte[] byteArray) throws IOException {
280 this.outputStream.write(byteArray);
281 if (immediateFlush) {
282 this.outputStream.flush();
283 }
284 }
285
286
287
288
289
290
291
292
293
294 protected void subAppend(E event) {
295 if (!isStarted()) {
296 return;
297 }
298 try {
299
300 if (event instanceof DeferredProcessingAware) {
301 ((DeferredProcessingAware) event).prepareForDeferredProcessing();
302 }
303 writeOut(event);
304
305 } catch (IOException ioe) {
306
307
308 this.started = false;
309 addStatus(new ErrorStatus("IO failure in appender", this, ioe));
310 }
311 }
312
313 public Encoder<E> getEncoder() {
314 return encoder;
315 }
316
317 public void setEncoder(Encoder<E> encoder) {
318 this.encoder = encoder;
319 }
320
321 public boolean isImmediateFlush() {
322 return immediateFlush;
323 }
324
325 public void setImmediateFlush(boolean immediateFlush) {
326 this.immediateFlush = immediateFlush;
327 }
328
329 }