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