KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > OnlyOnceErrorHandler


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.helpers;
18
19 import org.apache.log4j.spi.ErrorHandler;
20 import org.apache.log4j.spi.LoggingEvent;
21 import org.apache.log4j.Logger;
22 import org.apache.log4j.Appender;
23
24 /**
25
26    The <code>OnlyOnceErrorHandler</code> implements log4j's default
27    error handling policy which consists of emitting a message for the
28    first error in an appender and ignoring all following errors.
29
30    <p>The error message is printed on <code>System.err</code>.
31
32    <p>This policy aims at protecting an otherwise working application
33    from being flooded with error messages when logging fails.
34
35    @author Ceki G&uuml;lc&uuml;
36    @since 0.9.0 */

37 public class OnlyOnceErrorHandler implements ErrorHandler {
38
39
40   final String JavaDoc WARN_PREFIX = "log4j warning: ";
41   final String JavaDoc ERROR_PREFIX = "log4j error: ";
42
43   boolean firstTime = true;
44
45
46   /**
47      Does not do anything.
48    */

49   public
50   void setLogger(Logger logger) {
51   }
52
53
54   /**
55      No options to activate.
56   */

57   public
58   void activateOptions() {
59   }
60
61
62   /**
63      Prints the message and the stack trace of the exception on
64      <code>System.err</code>. */

65   public
66   void error(String JavaDoc message, Exception JavaDoc e, int errorCode) {
67     error(message, e, errorCode, null);
68   }
69
70   /**
71      Prints the message and the stack trace of the exception on
72      <code>System.err</code>.
73    */

74   public
75   void error(String JavaDoc message, Exception JavaDoc e, int errorCode, LoggingEvent event) {
76     if(firstTime) {
77       LogLog.error(message, e);
78       firstTime = false;
79     }
80   }
81
82
83   /**
84      Print a the error message passed as parameter on
85      <code>System.err</code>.
86   */

87   public
88   void error(String JavaDoc message) {
89     if(firstTime) {
90       LogLog.error(message);
91       firstTime = false;
92     }
93   }
94   
95   /**
96      Does not do anything.
97    */

98   public
99   void setAppender(Appender appender) {
100   }
101
102   /**
103      Does not do anything.
104    */

105   public
106   void setBackupAppender(Appender appender) {
107   }
108 }
109
Popular Tags