View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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   * OutputStreamAppender appends events to a {@link OutputStream}. This class
30   * provides basic services that other appenders build upon.
31   * 
32   * For more information about this appender, please refer to the online manual
33   * at http://logback.qos.ch/manual/appenders.html#OutputStreamAppender
34   * 
35   * @author Ceki Gülcü
36   */
37  public class OutputStreamAppender<E> extends UnsynchronizedAppenderBase<E> {
38  
39      /**
40       * It is the encoder which is ultimately responsible for writing the event to an
41       * {@link OutputStream}.
42       */
43      protected Encoder<E> encoder;
44  
45      /**
46       * All synchronization in this class is done via the lock object.
47       */
48      protected final ReentrantLock streamWriteLock = new ReentrantLock(false);
49  
50      /**
51       * This is the {@link OutputStream outputStream} where output will be written.
52       */
53      private OutputStream outputStream;
54  
55      boolean immediateFlush = true;
56  
57      boolean statefulEncoder = false;
58  
59      /**
60       * The underlying output stream used by this appender.
61       * 
62       * @return
63       */
64      public OutputStream getOutputStream() {
65          return outputStream;
66      }
67  
68      /**
69       * Checks that requires parameters are set and if everything is in order,
70       * activates this appender.
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          // only error free appenders should be activated
87          if (errors == 0) {
88              this.statefulEncoder = encoder.isStateful();
89              // Hold the lock across started=true and headerBytes so a concurrent
90              // append cannot encode before the header is written.
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      * Stop this appender instance. The underlying stream or writer is also closed.
122      *
123      * <p>The encoder is stopped as well, after the footer has been written and the stream closed, so that any
124      * resources it holds (for instance per-thread buffers) are released. Otherwise, each context reset or
125      * reconfiguration would leave the previous encoder and its resources behind.</p>
126      *
127      * <p>Stopped appenders cannot be reused.</p>
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      * Stop the encoder, if it is started.
145      *
146      * <p>It is assumed that the caller has acquired the streamWriteLock.</p>
147      */
148     private void stopEncoder() {
149         if (encoder != null && encoder.isStarted()) {
150             encoder.stop();
151         }
152     }
153 
154     /**
155      * Close the underlying {@link OutputStream}.
156      */
157     protected void closeOutputStream() {
158         if (this.outputStream != null) {
159             try {
160                 // before closing we have to output out encooder's footer
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      * Write the encoder's footer to the underlying {@link OutputStream}.
172      *
173      * <p>It is assumed that the caller has acquired the streamWriteLock.</p>
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      * <p>
189      * Sets the {@link OutputStream} where the log output will go. The specified
190      * <code>OutputStream</code> must be opened by the user and be writable. The
191      * <code>OutputStream</code> will be closed when the appender instance is
192      * closed.
193      * </p>
194      *
195      * @param outputStream An already opened OutputStream.
196      */
197     public void setOutputStream(OutputStream outputStream) {
198         streamWriteLock.lock();
199         try {
200             // close any previously opened output stream
201             closeOutputStream();
202             this.outputStream = outputStream;
203 
204             // the first call to setOutputStream() is made on a non started appender
205             // However, in subsequent calls to setOutputStream() the appender will be in
206             // started state. In subsequent calls, in particular when opening a file after rollover,
207             // we have to output the header hence the call to encoderInit().
208             if(isStarted()) {
209                 encoderInit();
210             }
211         } finally {
212             streamWriteLock.unlock();
213         }
214     }
215 
216     /**
217      * <p>Write the encoder's header to the underlying {@link OutputStream}.</p>
218      *
219      * <p>It is assumed that the caller has acquired the streamWriteLock.</p>
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             // Recheck under the lock: stop() may have written the footer since
245             // subAppend() observed isStarted().
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             // guard against appender with stop() invoked in parallel
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      * A simple method to write to an outputStream and flush the stream if immediateFlush is set to true.
276      *
277      * @since 1.3.9/1.4.9
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      * Actual writing occurs here.
288      * <p>
289      * Most subclasses of <code>WriterAppender</code> will need to override this
290      * method.
291      * 
292      * @since 0.9.0
293      */
294     protected void subAppend(E event) {
295         if (!isStarted()) {
296             return;
297         }
298         try {
299             // this step avoids LBCLASSIC-139
300             if (event instanceof DeferredProcessingAware) {
301                 ((DeferredProcessingAware) event).prepareForDeferredProcessing();
302             }
303             writeOut(event);
304 
305         } catch (IOException ioe) {
306             // as soon as an exception occurs, move to non-started state
307             // and add a single ErrorStatus to the SM.
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 }