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         append(event);
118     }
119 
120     /**
121      * Set the {@link ErrorHandler} for this Appender.
122      *
123      * @since 0.9.0
124      */
125     @Override
126     public synchronized void setErrorHandler(final ErrorHandler eh) {
127         if (eh != null) {
128             this.errorHandler = eh;
129         }
130     }
131 
132     @Override
133     public void setLayout(final Layout layout) {
134         this.layout = layout;
135     }
136 
137     @Override
138     public void setName(final String name) {
139         this.name = name;
140     }
141 
142     public void setThreshold(final Priority threshold) {
143         this.threshold = threshold;
144     }
145 
146     public static class NoOpErrorHandler implements ErrorHandler {
147         @Override
148         public void setLogger(final Logger logger) {
149 
150         }
151 
152         @Override
153         public void error(final String message, final Exception e, final int errorCode) {
154 
155         }
156 
157         @Override
158         public void error(final String message) {
159 
160         }
161 
162         @Override
163         public void error(final String message, final Exception e, final int errorCode, final LoggingEvent event) {
164 
165         }
166 
167         @Override
168         public void setAppender(final Appender appender) {
169 
170         }
171 
172         @Override
173         public void setBackupAppender(final Appender appender) {
174 
175         }
176     }
177 }