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.logging.log4j.core.appender;
18  
19  import java.io.Serializable;
20  
21  import org.apache.logging.log4j.core.Appender;
22  import org.apache.logging.log4j.core.ErrorHandler;
23  import org.apache.logging.log4j.core.Filter;
24  import org.apache.logging.log4j.core.Layout;
25  import org.apache.logging.log4j.core.LogEvent;
26  import org.apache.logging.log4j.core.filter.AbstractFilterable;
27  import org.apache.logging.log4j.core.helpers.Integers;
28  
29  /**
30   * Abstract base class for Appenders. Although Appenders do not have to extend this class, doing so
31   * will simplify their implementation.
32   */
33  public abstract class AbstractAppender extends AbstractFilterable
34      implements Appender {
35      private final boolean ignoreExceptions;
36  
37      private ErrorHandler handler = new DefaultErrorHandler(this);
38  
39      private final Layout<? extends Serializable> layout;
40  
41      private final String name;
42  
43      /**
44       * Appenders set this by calling super.start().
45       */
46      private boolean started = false;
47  
48      public static int parseInt(String s, int defaultValue) {
49          try {
50              return Integers.parseInt(s, defaultValue);
51          } catch (NumberFormatException e) {
52              LOGGER.error("Could not parse \"{}\" as an integer,  using default value {}: {}", s, defaultValue, e);
53              return defaultValue;
54          }
55      }
56  
57      /**
58       * Constructor that defaults to suppressing exceptions.
59       * @param name The Appender name.
60       * @param filter The Filter to associate with the Appender.
61       * @param layout The layout to use to format the event.
62       */
63      protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout) {
64          this(name, filter, layout, true);
65      }
66  
67      /**
68       * Constructor.
69       * @param name The Appender name.
70       * @param filter The Filter to associate with the Appender.
71       * @param layout The layout to use to format the event.
72       * @param ignoreExceptions If true, exceptions will be logged and suppressed. If false errors will be
73       * logged and then passed to the application.
74       */
75      protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
76                                 final boolean ignoreExceptions) {
77          super(filter);
78          this.name = name;
79          this.layout = layout;
80          this.ignoreExceptions = ignoreExceptions;
81      }
82  
83      /**
84       * Handle an error with a message.
85       * @param msg The message.
86       */
87      public void error(final String msg) {
88          handler.error(msg);
89      }
90  
91      /**
92       * Handle an error with a message, and exception and a logging event.
93       * @param msg The message.
94       * @param event The LogEvent.
95       * @param t The Throwable.
96       */
97      public void error(final String msg, final LogEvent event, final Throwable t) {
98          handler.error(msg, event, t);
99      }
100 
101     /**
102      * Handle an error with a message and an exception.
103      * @param msg The message.
104      * @param t The Throwable.
105      */
106     public void error(final String msg, final Throwable t) {
107         handler.error(msg, t);
108     }
109 
110     /**
111      * Returns the ErrorHandler, if any.
112      * @return The ErrorHandler.
113      */
114     @Override
115     public ErrorHandler getHandler() {
116         return handler;
117     }
118 
119     /**
120      * Returns the Layout for the appender.
121      * @return The Layout used to format the event.
122      */
123     @Override
124     public Layout<? extends Serializable> getLayout() {
125         return layout;
126     }
127 
128     /**
129      * Returns the name of the Appender.
130      * @return The name of the Appender.
131      */
132     @Override
133     public String getName() {
134         return name;
135     }
136 
137     /**
138      * Some appenders need to propagate exceptions back to the application. When {@code ignoreExceptions} is
139      * {@code false} the AppenderControl will allow the exception to percolate.
140      *
141      * @return {@code true} if exceptions will be logged but now thrown, {@code false} otherwise.
142      */
143     @Override
144     public boolean ignoreExceptions() {
145         return ignoreExceptions;
146     }
147 
148     /**
149      * Returns true if the Appender is started, false otherwise.
150      * @return true if the Appender is started, false otherwise.
151      */
152     @Override
153     public boolean isStarted() {
154         return started;
155     }
156 
157     /**
158      * The handler must be set before the appender is started.
159      * @param handler The ErrorHandler to use.
160      */
161     @Override
162     public void setHandler(final ErrorHandler handler) {
163         if (handler == null) {
164             LOGGER.error("The handler cannot be set to null");
165         }
166         if (isStarted()) {
167             LOGGER.error("The handler cannot be changed once the appender is started");
168             return;
169         }
170         this.handler = handler;
171     }
172 
173     /**
174      * Start the Appender.
175      */
176     @Override
177     public void start() {
178         startFilter();
179         this.started = true;
180     }
181 
182     /**
183      * Stop the Appender.
184      */
185     @Override
186     public void stop() {
187         this.started = false;
188         stopFilter();
189     }
190 
191     @Override
192     public String toString() {
193         return name;
194     }
195 
196 }