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