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