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.util.concurrent.TimeUnit;
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 {
31
32 private static final Logger LOGGER = StatusLogger.getLogger();
33
34 private static final int MAX_EXCEPTIONS = 3;
35
36 private static final long EXCEPTION_INTERVAL = TimeUnit.MINUTES.toNanos(5);
37
38 private int exceptionCount = 0;
39
40 private long lastException = System.nanoTime() - EXCEPTION_INTERVAL - 1;
41
42 private final Appender appender;
43
44 public DefaultErrorHandler(final Appender appender) {
45 this.appender = appender;
46 }
47
48
49
50
51
52
53 @Override
54 public void error(final String msg) {
55 final long current = System.nanoTime();
56 if (current - lastException > EXCEPTION_INTERVAL || exceptionCount++ < MAX_EXCEPTIONS) {
57 LOGGER.error(msg);
58 }
59 lastException = current;
60 }
61
62
63
64
65
66
67 @Override
68 public void error(final String msg, final Throwable t) {
69 final long current = System.nanoTime();
70 if (current - lastException > EXCEPTION_INTERVAL || exceptionCount++ < MAX_EXCEPTIONS) {
71 LOGGER.error(msg, t);
72 }
73 lastException = current;
74 if (!appender.ignoreExceptions() && t != null && !(t instanceof AppenderLoggingException)) {
75 throw new AppenderLoggingException(msg, t);
76 }
77 }
78
79
80
81
82
83
84
85 @Override
86 public void error(final String msg, final LogEvent event, final Throwable t) {
87 final long current = System.nanoTime();
88 if (current - lastException > EXCEPTION_INTERVAL || exceptionCount++ < MAX_EXCEPTIONS) {
89 LOGGER.error(msg, t);
90 }
91 lastException = current;
92 if (!appender.ignoreExceptions() && t != null && !(t instanceof AppenderLoggingException)) {
93 throw new AppenderLoggingException(msg, t);
94 }
95 }
96
97 public Appender getAppender() {
98 return appender;
99 }
100 }