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