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 java.io.Serializable;
20  import java.util.concurrent.locks.Lock;
21  import java.util.concurrent.locks.ReadWriteLock;
22  import java.util.concurrent.locks.ReentrantReadWriteLock;
23  
24  import org.apache.logging.log4j.core.Filter;
25  import org.apache.logging.log4j.core.Layout;
26  import org.apache.logging.log4j.core.LogEvent;
27  
28  /**
29   * Appends log events as bytes to a byte output stream. The stream encoding is defined in the layout.
30   * 
31   * @param <M> The kind of {@link OutputStreamManager} under management
32   */
33  public abstract class AbstractOutputStreamAppender<M extends OutputStreamManager> extends AbstractAppender {
34  
35      /**
36       * Immediate flush means that the underlying writer or output stream
37       * will be flushed at the end of each append operation. Immediate
38       * flush is slower but ensures that each append request is actually
39       * written. If <code>immediateFlush</code> is set to
40       * {@code false}, then there is a good chance that the last few
41       * logs events are not actually written to persistent media if and
42       * when the application crashes.
43       */
44      protected final boolean immediateFlush;
45  
46      private final M manager;
47  
48      private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
49      private final Lock readLock = rwLock.readLock();
50      
51      /**
52       * Instantiate a WriterAppender and set the output destination to a
53       * new {@link java.io.OutputStreamWriter} initialized with <code>os</code>
54       * as its {@link java.io.OutputStream}.
55       * @param name The name of the Appender.
56       * @param layout The layout to format the message.
57       * @param manager The OutputStreamManager.
58       */
59      protected AbstractOutputStreamAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
60                                             final boolean ignoreExceptions, final boolean immediateFlush,
61                                             final M manager) {
62          super(name, filter, layout, ignoreExceptions);
63          this.manager = manager;
64          this.immediateFlush = immediateFlush;
65      }
66  
67      /**
68       * Gets the manager.
69       * 
70       * @return the manager.
71       */
72      public M getManager() {
73          return manager;
74      }
75  
76      @Override
77      public void start() {
78          if (getLayout() == null) {
79              LOGGER.error("No layout set for the appender named [" + getName() + "].");
80          }
81          if (manager == null) {
82              LOGGER.error("No OutputStreamManager set for the appender named [" + getName() + "].");
83          }
84          super.start();
85      }
86  
87      @Override
88      public void stop() {
89          super.stop();
90          manager.release();
91      }
92  
93      /**
94       * Actual writing occurs here.
95       * <p/>
96       * <p>Most subclasses of <code>AbstractOutputStreamAppender</code> will need to
97       * override this method.
98       * @param event The LogEvent.
99       */
100     @Override
101     public void append(final LogEvent event) {
102         readLock.lock();
103         try {
104             final byte[] bytes = getLayout().toByteArray(event);
105             if (bytes.length > 0) {
106                 manager.write(bytes);
107                 if (this.immediateFlush || event.isEndOfBatch()) {
108                     manager.flush();
109                 }
110             }
111         } catch (final AppenderLoggingException ex) {
112             error("Unable to write to stream " + manager.getName() + " for appender " + getName());
113             throw ex;
114         } finally {
115             readLock.unlock();
116         }
117     }
118 }