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