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(Appender appender) {
43 this.appender = appender;
44 }
45
46
47
48
49
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
61
62
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
77
78
79
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 }