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