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