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  
21  import org.apache.logging.log4j.core.Appender;
22  import org.apache.logging.log4j.core.ErrorHandler;
23  import org.apache.logging.log4j.core.Filter;
24  import org.apache.logging.log4j.core.LifeCycle;
25  import org.apache.logging.log4j.core.LogEvent;
26  import org.apache.logging.log4j.core.Layout;
27  import org.apache.logging.log4j.core.filter.AbstractFilterable;
28  import org.apache.logging.log4j.status.StatusLogger;
29  import org.apache.logging.log4j.Logger;
30  
31  /**
32   * Abstract base class for Appenders. Although Appenders do not have to extend this class, doing so
33   * will simplify their implementation.
34   *
35   * @param <T> The {@link Layout}'s {@link Serializable} type.
36   */
37  public abstract class AbstractAppender<T extends Serializable> extends AbstractFilterable
38      implements Appender<T>, LifeCycle {
39      /**
40       * Allow subclasses access to the status logger without creating another instance.
41       */
42      protected static final Logger LOGGER = StatusLogger.getLogger();
43  
44      /**
45       * Appenders set this by calling super.start().
46       */
47      private boolean started = false;
48  
49      private final Layout<T> layout;
50  
51      private final String name;
52  
53      private final boolean handleException;
54  
55      private ErrorHandler handler = new DefaultErrorHandler(this);
56  
57      /**
58       * Constructor that defaults to suppressing exceptions.
59       * @param name The Appender name.
60       * @param filter The Filter to associate with the Appender.
61       * @param layout The layout to use to format the event.
62       */
63      protected AbstractAppender(final String name, final Filter filter, final Layout<T> layout) {
64          this(name, filter, layout, true);
65      }
66  
67      /**
68       * Constructor.
69       * @param name The Appender name.
70       * @param filter The Filter to associate with the Appender.
71       * @param layout The layout to use to format the event.
72       * @param handleException If true, exceptions will be logged and suppressed. If false errors will be
73       * logged and then passed to the application.
74       */
75      protected AbstractAppender(final String name, final Filter filter, final Layout<T> layout,
76                                 final boolean handleException) {
77          super(filter);
78          this.name = name;
79          this.layout = layout;
80          this.handleException = handleException;
81      }
82  
83      /**
84       * Returns the ErrorHandler, if any.
85       * @return The ErrorHandler.
86       */
87      public ErrorHandler getHandler() {
88          return handler;
89      }
90  
91      /**
92       * The handler must be set before the appender is started.
93       * @param handler The ErrorHandler to use.
94       */
95      public void setHandler(final ErrorHandler handler) {
96          if (handler == null) {
97              LOGGER.error("The handler cannot be set to null");
98          }
99          if (isStarted()) {
100             LOGGER.error("The handler cannot be changed once the appender is started");
101             return;
102         }
103         this.handler = handler;
104     }
105 
106     /**
107      * Close the stream associated with the Appender.
108      */
109     public void close() {
110 
111     }
112 
113     /**
114      * Returns the name of the Appender.
115      * @return The name of the Appender.
116      */
117     public String getName() {
118         return name;
119     }
120 
121     /**
122      * Returns the Layout for the appender.
123      * @return The Layout used to format the event.
124      */
125     public Layout<T> getLayout() {
126         return layout;
127     }
128 
129     /**
130      * Some appenders need to propagate exceptions back to the application. When suppressException is false the
131      * AppenderControl will allow the exception to percolate.
132      * @return true if exceptions will be suppressed, false otherwise.
133      */
134     public boolean isExceptionSuppressed() {
135         return handleException;
136     }
137 
138     /**
139      * Start the Appender.
140      */
141     public void start() {
142         startFilter();
143         this.started = true;
144     }
145 
146     /**
147      * Stop the Appender.
148      */
149     public void stop() {
150         this.started = false;
151         stopFilter();
152     }
153 
154     /**
155      * Returns true if the Appender is started, false otherwise.
156      * @return true if the Appender is started, false otherwise.
157      */
158     public boolean isStarted() {
159         return started;
160     }
161 
162     @Override
163     public String toString() {
164         return name;
165     }
166 
167     /**
168      * Handle an error with a message.
169      * @param msg The message.
170      */
171     public void error(final String msg) {
172         handler.error(msg);
173     }
174 
175     /**
176      * Handle an error with a message and an exception.
177      * @param msg The message.
178      * @param t The Throwable.
179      */
180     public void error(final String msg, final Throwable t) {
181         handler.error(msg, t);
182     }
183 
184     /**
185      * Handle an error with a message, and exception and a logging event.
186      * @param msg The message.
187      * @param event The LogEvent.
188      * @param t The Throwable.
189      */
190     public void error(final String msg, final LogEvent event, final Throwable t) {
191         handler.error(msg, event, t);
192     }
193 
194 }