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