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