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      public void activateOptions() {
55      }
56  
57      public void addFilter(Filter newFilter) {
58          if(headFilter == null) {
59              headFilter = tailFilter = newFilter;
60          } else {
61              tailFilter.setNext(newFilter);
62              tailFilter = newFilter;
63          }
64      }
65  
66      protected abstract void append(LoggingEvent event);
67  
68      public void clearFilters() {
69          headFilter = tailFilter = null;
70      }
71  
72      public void finalize() {
73      }
74  
75      public ErrorHandler getErrorHandler() {
76          return this.errorHandler;
77      }
78  
79      public Filter getFilter() {
80          return headFilter;
81      }
82  
83      public final Filter getFirstFilter() {
84          return headFilter;
85      }
86  
87      public Layout getLayout() {
88          return layout;
89      }
90  
91      public final String getName() {
92          return this.name;
93      }
94  
95      public Priority getThreshold() {
96          return threshold;
97      }
98  
99      public boolean isAsSevereAsThreshold(Priority priority) {
100         return ((threshold == null) || priority.isGreaterOrEqual(threshold));
101     }
102 
103     /**
104      * This method is never going to be called in Log4j 2 so there isn't much point in having any code in it.
105      * @param event The LoggingEvent.
106      */
107     public void doAppend(LoggingEvent event) {
108     }
109 
110     /**
111      * Set the {@link ErrorHandler} for this Appender.
112      *
113      * @since 0.9.0
114      */
115     public synchronized void setErrorHandler(ErrorHandler eh) {
116         if (eh != null) {
117             this.errorHandler = eh;
118         }
119     }
120 
121     public void setLayout(Layout layout) {
122         this.layout = layout;
123     }
124 
125     public void setName(String name) {
126         this.name = name;
127     }
128 
129     public void setThreshold(Priority threshold) {
130         this.threshold = threshold;
131     }
132 
133     public static class NoOpErrorHandler implements ErrorHandler {
134         @Override
135         public void setLogger(Logger logger) {
136 
137         }
138 
139         @Override
140         public void error(String message, Exception e, int errorCode) {
141 
142         }
143 
144         @Override
145         public void error(String message) {
146 
147         }
148 
149         @Override
150         public void error(String message, Exception e, int errorCode, LoggingEvent event) {
151 
152         }
153 
154         @Override
155         public void setAppender(Appender appender) {
156 
157         }
158 
159         @Override
160         public void setBackupAppender(Appender appender) {
161 
162         }
163     }
164 }