View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender;
18  
19  import org.apache.logging.log4j.core.Filter;
20  import org.apache.logging.log4j.core.Layout;
21  import org.apache.logging.log4j.core.LogEvent;
22  
23  import java.util.concurrent.locks.Lock;
24  import java.util.concurrent.locks.ReadWriteLock;
25  import java.util.concurrent.locks.ReentrantReadWriteLock;
26  
27  /**
28   * Appends log events as bytes to a byte output stream. The stream encoding is defined in the layout.
29   */
30  public abstract class AbstractOutputStreamAppender extends AbstractAppender {
31  
32      /**
33       * Immediate flush means that the underlying writer or output stream
34       * will be flushed at the end of each append operation. Immediate
35       * flush is slower but ensures that each append request is actually
36       * written. If <code>immediateFlush</code> is set to
37       * {@code false}, then there is a good chance that the last few
38       * logs events are not actually written to persistent media if and
39       * when the application crashes.
40       */
41      protected final boolean immediateFlush;
42  
43      private volatile OutputStreamManager manager;
44  
45      private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
46      private final Lock readLock = rwLock.readLock();
47      private final Lock writeLock = rwLock.writeLock();
48  
49      /**
50       * Instantiate a WriterAppender and set the output destination to a
51       * new {@link java.io.OutputStreamWriter} initialized with <code>os</code>
52       * as its {@link java.io.OutputStream}.
53       * @param name The name of the Appender.
54       * @param layout The layout to format the message.
55       * @param manager The OutputStreamManager.
56       */
57      protected AbstractOutputStreamAppender(String name, Layout layout, Filter filter, boolean handleException,
58                                  boolean immediateFlush, OutputStreamManager manager) {
59          super(name, filter, layout, handleException);
60          if (layout != null) {
61              manager.setHeader(layout.getHeader());
62              manager.setFooter(layout.getFooter());
63          }
64          this.manager = manager;
65          this.immediateFlush = immediateFlush;
66      }
67  
68      protected OutputStreamManager getManager() {
69          return manager;
70      }
71  
72      protected void replaceManager(OutputStreamManager newManager) {
73  
74          writeLock.lock();
75          try {
76              OutputStreamManager old = manager;
77              manager = newManager;
78              old.release();
79          } finally {
80              writeLock.unlock();
81          }
82  
83      }
84  
85      @Override
86      public void start() {
87          if (getLayout() == null) {
88              LOGGER.error("No layout set for the appender named [" + getName() + "].");
89          }
90          if (manager == null) {
91              LOGGER.error("No OutputStreamManager set for the appender named [" + getName() + "].");
92          }
93          super.start();
94      }
95  
96      @Override
97      public void stop() {
98          super.stop();
99          manager.release();
100     }
101 
102     /**
103      * Actual writing occurs here.
104      * <p/>
105      * <p>Most subclasses of <code>AbstractOutputStreamAppender</code> will need to
106      * override this method.
107      * @param event The LogEvent.
108      */
109     public void append(LogEvent event) {
110         readLock.lock();
111         try {
112             manager.write(getLayout().toByteArray(event));
113             if (this.immediateFlush) {
114                 manager.flush();
115             }
116         } catch (AppenderRuntimeException ex) {
117             error("Unable to write to stream " + manager.getName() + " for appender " + getName());
118             throw ex;
119         } finally {
120             readLock.unlock();
121         }
122     }
123 }