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