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.log4j;
18  
19  import org.apache.log4j.spi.ErrorHandler;
20  import org.apache.log4j.spi.Filter;
21  import org.apache.log4j.spi.LoggingEvent;
22  import org.apache.log4j.spi.OptionHandler;
23  
24  /**
25   * The base class for Appenders in Log4j 1. Appenders constructed using this are ignored in Log4j 2.
26   */
27  public abstract class AppenderSkeleton implements Appender, OptionHandler {
28  
29      protected Layout layout;
30  
31      protected String name;
32  
33      protected Priority threshold;
34  
35      protected ErrorHandler errorHandler = new NoOpErrorHandler();
36  
37      protected Filter headFilter;
38  
39      protected Filter tailFilter;
40  
41      protected boolean closed = false;
42  
43      /**
44       * Create new instance.
45       */
46      public AppenderSkeleton() {
47          super();
48      }
49  
50      protected AppenderSkeleton(final boolean isActive) {
51          super();
52      }
53  
54      @Override
55      public void activateOptions() {
56      }
57  
58      @Override
59      public void addFilter(final Filter newFilter) {
60          if(headFilter == null) {
61              headFilter = tailFilter = newFilter;
62          } else {
63              tailFilter.setNext(newFilter);
64              tailFilter = newFilter;
65          }
66      }
67  
68      protected abstract void append(LoggingEvent event);
69  
70      @Override
71      public void clearFilters() {
72          headFilter = tailFilter = null;
73      }
74  
75      @Override
76      public void finalize() {
77      }
78  
79      @Override
80      public ErrorHandler getErrorHandler() {
81          return this.errorHandler;
82      }
83  
84      @Override
85      public Filter getFilter() {
86          return headFilter;
87      }
88  
89      public final Filter getFirstFilter() {
90          return headFilter;
91      }
92  
93      @Override
94      public Layout getLayout() {
95          return layout;
96      }
97  
98      @Override
99      public final String getName() {
100         return this.name;
101     }
102 
103     public Priority getThreshold() {
104         return threshold;
105     }
106 
107     public boolean isAsSevereAsThreshold(final Priority priority) {
108         return ((threshold == null) || priority.isGreaterOrEqual(threshold));
109     }
110 
111     /**
112      * This method is never going to be called in Log4j 2 so there isn't much point in having any code in it.
113      * @param event The LoggingEvent.
114      */
115     @Override
116     public void doAppend(final LoggingEvent event) {
117     }
118 
119     /**
120      * Set the {@link ErrorHandler} for this Appender.
121      *
122      * @since 0.9.0
123      */
124     @Override
125     public synchronized void setErrorHandler(final ErrorHandler eh) {
126         if (eh != null) {
127             this.errorHandler = eh;
128         }
129     }
130 
131     @Override
132     public void setLayout(final Layout layout) {
133         this.layout = layout;
134     }
135 
136     @Override
137     public void setName(final String name) {
138         this.name = name;
139     }
140 
141     public void setThreshold(final Priority threshold) {
142         this.threshold = threshold;
143     }
144 
145     public static class NoOpErrorHandler implements ErrorHandler {
146         @Override
147         public void setLogger(final Logger logger) {
148 
149         }
150 
151         @Override
152         public void error(final String message, final Exception e, final int errorCode) {
153 
154         }
155 
156         @Override
157         public void error(final String message) {
158 
159         }
160 
161         @Override
162         public void error(final String message, final Exception e, final int errorCode, final LoggingEvent event) {
163 
164         }
165 
166         @Override
167         public void setAppender(final Appender appender) {
168 
169         }
170 
171         @Override
172         public void setBackupAppender(final Appender appender) {
173 
174         }
175     }
176 }