1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
54
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
67
68
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
84
85
86
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 }