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(final String name, final Layout layout, final Filter filter,
58                                             final boolean handleException, final boolean immediateFlush,
59                                             final OutputStreamManager manager) {
60          super(name, filter, layout, handleException);
61          if (layout != null) {
62              manager.setHeader(layout.getHeader());
63              manager.setFooter(layout.getFooter());
64          }
65          this.manager = manager;
66          this.immediateFlush = immediateFlush;
67      }
68  
69      protected OutputStreamManager getManager() {
70          return manager;
71      }
72  
73      protected void replaceManager(final OutputStreamManager newManager) {
74  
75          writeLock.lock();
76          try {
77              final OutputStreamManager old = manager;
78              manager = newManager;
79              old.release();
80          } finally {
81              writeLock.unlock();
82          }
83  
84      }
85  
86      @Override
87      public void start() {
88          if (getLayout() == null) {
89              LOGGER.error("No layout set for the appender named [" + getName() + "].");
90          }
91          if (manager == null) {
92              LOGGER.error("No OutputStreamManager set for the appender named [" + getName() + "].");
93          }
94          super.start();
95      }
96  
97      @Override
98      public void stop() {
99          super.stop();
100         manager.release();
101     }
102 
103     /**
104      * Actual writing occurs here.
105      * <p/>
106      * <p>Most subclasses of <code>AbstractOutputStreamAppender</code> will need to
107      * override this method.
108      * @param event The LogEvent.
109      */
110     public void append(final LogEvent event) {
111         readLock.lock();
112         try {
113             final byte[] bytes = getLayout().toByteArray(event);
114             if (bytes.length > 0) {
115                 manager.write(bytes);
116                 if (this.immediateFlush) {
117                     manager.flush();
118                 }
119             }
120         } catch (final AppenderRuntimeException ex) {
121             error("Unable to write to stream " + manager.getName() + " for appender " + getName());
122             throw ex;
123         } finally {
124             readLock.unlock();
125         }
126     }
127 }