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.Logger;
    22  import org.apache.logging.log4j.core.Appender;
    23  import org.apache.logging.log4j.core.ErrorHandler;
    24  import org.apache.logging.log4j.core.LogEvent;
    25  import org.apache.logging.log4j.status.StatusLogger;
    26
    27  /**
    28   *
    29   */
    30  public class DefaultErrorHandler implements ErrorHandler, Serializable {
    31
    32      private static final long serialVersionUID = 1L;
    33
    34      private static final Logger LOGGER = StatusLogger.getLogger();
    35
    36      private static final int MAX_EXCEPTIONS = 3;
    37
    38      private static final int EXCEPTION_INTERVAL = 300000;
    39
    40      private int exceptionCount = 0;
    41
    42      private long lastException;
    43
    44      private final Appender appender;
    45
    46      public DefaultErrorHandler(final Appender appender) {
    47          this.appender = appender;
    48      }
    49
    50
    51      /**
    52       * Handle an error with a message.
    53       * @param msg The message.
    54       */
    55      @Override
    56      public void error(final String msg) {
    57          final long current = System.currentTimeMillis();
    58          if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
    59              LOGGER.error(msg);
    60          }
    61          lastException = current;
    62      }
    63
    64      /**
    65       * Handle an error with a message and an exception.
    66       * @param msg The message.
    67       * @param t The Throwable.
    68       */
    69      @Override
    70      public void error(final String msg, final Throwable t) {
    71          final long current = System.currentTimeMillis();
    72          if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
    73              LOGGER.error(msg, t);
    74          }
    75          lastException = current;
    76          if (!appender.ignoreExceptions() && t != null && !(t instanceof AppenderLoggingException)) {
    77              throw new AppenderLoggingException(msg, t);
    78          }
    79      }
    80
    81      /**
    82       * Handle an error with a message, and exception and a logging event.
    83       * @param msg The message.
    84       * @param event The LogEvent.
    85       * @param t The Throwable.
    86       */
    87      @Override
    88      public void error(final String msg, final LogEvent event, final Throwable t) {
    89          final long current = System.currentTimeMillis();
    90          if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
    91              LOGGER.error(msg, t);
    92          }
    93          lastException = current;
    94          if (!appender.ignoreExceptions() && t != null && !(t instanceof AppenderLoggingException)) {
    95              throw new AppenderLoggingException(msg, t);
    96          }
    97      }
    98  }