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