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