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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j;
20 import org.apache.log4j.spi.Filter;
21 import org.apache.log4j.spi.ErrorHandler;
22 import org.apache.log4j.spi.OptionHandler;
23 import org.apache.log4j.spi.LoggingEvent;
24 import org.apache.log4j.helpers.OnlyOnceErrorHandler;
25 import org.apache.log4j.helpers.LogLog;
29 * Abstract superclass of the other appenders in the package.
31 * This class provides the code for common functionality, such as
32 * support for threshold filtering and support for general filters.
35 * @author Ceki Gülcü
37 public abstract class AppenderSkeleton implements Appender, OptionHandler {
39 /** The layout variable does not need to be set if the appender
40 implementation has its own layout. */
41 protected Layout layout;
43 /** Appenders are named. */
44 protected String name;
47 There is no level threshold filtering by default. */
48 protected Priority threshold;
51 It is assumed and enforced that errorHandler is never null.
53 protected ErrorHandler errorHandler = new OnlyOnceErrorHandler();
55 /** The first filter in the filter chain. Set to <code>null</code>
57 protected Filter headFilter;
58 /** The last filter in the filter chain. */
59 protected Filter tailFilter;
62 Is this appender closed?
64 protected boolean closed = false;
67 * Create new instance.
69 public AppenderSkeleton() {
74 * Create new instance.
75 * Provided for compatibility with log4j 1.3.
77 * @param isActive true if appender is ready for use upon construction.
78 * Not used in log4j 1.2.x.
81 protected AppenderSkeleton(final boolean isActive) {
88 Derived appenders should override this method if option structure
91 void activateOptions() {
96 Add a filter to end of the filter list.
101 void addFilter(Filter newFilter) {
102 if(headFilter == null) {
103 headFilter = tailFilter = newFilter;
105 tailFilter.setNext(newFilter);
106 tailFilter = newFilter;
111 Subclasses of <code>AppenderSkeleton</code> should implement this
112 method to perform actual logging. See also {@link #doAppend
113 AppenderSkeleton.doAppend} method.
119 void append(LoggingEvent event);
123 Clear the filters chain.
127 void clearFilters() {
128 headFilter = tailFilter = null;
132 Finalize this appender by calling the derived class'
133 <code>close</code> method.
138 // An appender might be closed then garbage collected. There is no
139 // point in closing twice.
144 LogLog.debug("Finalizing appender named ["+name+"].");
150 Return the currently set {@link ErrorHandler} for this
155 ErrorHandler getErrorHandler() {
156 return this.errorHandler;
161 Returns the head Filter.
171 Return the first filter in the filter chain for this
172 Appender. The return value may be <code>null</code> if no is
178 Filter getFirstFilter() {
183 Returns the layout of this appender. The value may be null.
192 Returns the name of this appender.
193 @return name, may be null.
202 Returns this appenders threshold level. See the {@link
203 #setThreshold} method for the meaning of this option.
207 Priority getThreshold() {
213 Check whether the message level is below the appender's
214 threshold. If there is no threshold set, then the return value is
215 always <code>true</code>.
219 boolean isAsSevereAsThreshold(Priority priority) {
220 return ((threshold == null) || priority.isGreaterOrEqual(threshold));
225 * This method performs threshold checks and invokes filters before
226 * delegating actual logging to the subclasses specific {@link
227 * AppenderSkeleton#append} method.
231 void doAppend(LoggingEvent event) {
233 LogLog.error("Attempted to append to closed appender named ["+name+"].");
237 if(!isAsSevereAsThreshold(event.getLevel())) {
241 Filter f = this.headFilter;
245 switch(f.decide(event)) {
246 case Filter.DENY: return;
247 case Filter.ACCEPT: break FILTER_LOOP;
248 case Filter.NEUTRAL: f = f.getNext();
256 Set the {@link ErrorHandler} for this Appender.
261 void setErrorHandler(ErrorHandler eh) {
263 // We do not throw exception here since the cause is probably a
265 LogLog.warn("You have tried to set a null error-handler.");
267 this.errorHandler = eh;
272 Set the layout for this appender. Note that some appenders have
273 their own (fixed) layouts or do not use one. For example, the
274 {@link org.apache.log4j.net.SocketAppender} ignores the layout set
278 void setLayout(Layout layout) {
279 this.layout = layout;
284 Set the name of this Appender.
287 void setName(String name) {
293 Set the threshold level. All log events with lower level
294 than the threshold level are ignored by the appender.
296 <p>In configuration files this option is specified by setting the
297 value of the <b>Threshold</b> option to a level
298 string, such as "DEBUG", "INFO" and so on.
302 void setThreshold(Priority threshold) {
303 this.threshold = threshold;