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(Appender appender) {
43          this.appender = appender;
44      }
45  
46  
47      /**
48       * Handle an error with a message.
49       * @param msg The message.
50       */
51      public void error(String msg) {
52          long current = System.currentTimeMillis();
53          if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
54              LOGGER.error(msg);
55          }
56          lastException = current;
57      }
58  
59      /**
60       * Handle an error with a message and an exception.
61       * @param msg The message.
62       * @param t The Throwable.
63       */
64      public void error(String msg, Throwable t) {
65          long current = System.currentTimeMillis();
66          if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
67              LOGGER.error(msg, t);
68          }
69          lastException = current;
70          if (!appender.isExceptionSuppressed() && t != null) {
71              throw new AppenderRuntimeException(msg, t);
72          }
73      }
74  
75      /**
76       * Handle an error with a message, and exception and a logging event.
77       * @param msg The message.
78       * @param event The LogEvent.
79       * @param t The Throwable.
80       */
81      public void error(String msg, LogEvent event, Throwable t) {
82          long current = System.currentTimeMillis();
83          if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
84              LOGGER.error(msg, t);
85          }
86          lastException = current;
87          if (!appender.isExceptionSuppressed() && t != null) {
88              throw new AppenderRuntimeException(msg, t);
89          }
90      }
91  }