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 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
49
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
62
63
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.isExceptionSuppressed() && t != null && !(t instanceof AppenderRuntimeException)) {
73 throw new AppenderRuntimeException(msg, t);
74 }
75 }
76
77
78
79
80
81
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.isExceptionSuppressed() && t != null && !(t instanceof AppenderRuntimeException)) {
91 throw new AppenderRuntimeException(msg, t);
92 }
93 }
94 }