1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.helpers;
19
20 import org.apache.log4j.spi.ErrorHandler;
21 import org.apache.log4j.spi.LoggingEvent;
22 import org.apache.log4j.Logger;
23 import org.apache.log4j.Appender;
24
25 /***
26
27 The <code>OnlyOnceErrorHandler</code> implements log4j's default
28 error handling policy which consists of emitting a message for the
29 first error in an appender and ignoring all following errors.
30
31 <p>The error message is printed on <code>System.err</code>.
32
33 <p>This policy aims at protecting an otherwise working application
34 from being flooded with error messages when logging fails.
35
36 @author Ceki Gülcü
37 @since 0.9.0 */
38 public class OnlyOnceErrorHandler implements ErrorHandler {
39
40
41 final String WARN_PREFIX = "log4j warning: ";
42 final String ERROR_PREFIX = "log4j error: ";
43
44 boolean firstTime = true;
45
46
47 /***
48 Does not do anything.
49 */
50 public
51 void setLogger(Logger logger) {
52 }
53
54
55 /***
56 No options to activate.
57 */
58 public
59 void activateOptions() {
60 }
61
62
63 /***
64 Prints the message and the stack trace of the exception on
65 <code>System.err</code>. */
66 public
67 void error(String message, Exception e, int errorCode) {
68 error(message, e, errorCode, null);
69 }
70
71 /***
72 Prints the message and the stack trace of the exception on
73 <code>System.err</code>.
74 */
75 public
76 void error(String message, Exception e, int errorCode, LoggingEvent event) {
77 if(firstTime) {
78 LogLog.error(message, e);
79 firstTime = false;
80 }
81 }
82
83
84 /***
85 Print a the error message passed as parameter on
86 <code>System.err</code>.
87 */
88 public
89 void error(String message) {
90 if(firstTime) {
91 LogLog.error(message);
92 firstTime = false;
93 }
94 }
95
96 /***
97 Does not do anything.
98 */
99 public
100 void setAppender(Appender appender) {
101 }
102
103 /***
104 Does not do anything.
105 */
106 public
107 void setBackupAppender(Appender appender) {
108 }
109 }