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