KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > util > OnlyOnceErrorHandler


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.logging.util;
24
25 import java.io.PrintStream JavaDoc;
26
27 import org.apache.log4j.spi.ErrorHandler;
28 import org.apache.log4j.spi.LoggingEvent;
29 import org.apache.log4j.Logger;
30 import org.apache.log4j.Appender;
31
32 /**
33
34    The <code>OnlyOnceErrorHandler</code> implements log4j's default
35    error handling policy which consists of emitting a message for the
36    first error in an appender and ignoring all following errors.
37
38    <p>The error message is printed on the specified print stream.
39
40    <p>This policy aims at protecting an otherwise working application
41    from being flooded with error messages when logging fails.
42
43    @author Ceki G&uuml;lc&uuml;
44    @author Adrian Brock (adrian@jboss.org);
45    @since 0.9.0 */

46 public class OnlyOnceErrorHandler implements ErrorHandler {
47
48
49   final String JavaDoc WARN_PREFIX = "log4j warning: ";
50   final String JavaDoc ERROR_PREFIX = "log4j error: ";
51
52   boolean firstTime = true;
53
54   static PrintStream JavaDoc output = System.err;
55
56   public static void setOutput(PrintStream JavaDoc out)
57   {
58      output = out;
59   }
60
61   /**
62      Does not do anything.
63    */

64   public
65   void setLogger(Logger logger) {
66   }
67
68
69   /**
70      No options to activate.
71   */

72   public
73   void activateOptions() {
74   }
75
76
77   /**
78      Prints the message and the stack trace of the exception on
79      <code>System.err</code>. */

80   public
81   void error(String JavaDoc message, Exception JavaDoc e, int errorCode) {
82     error(message, e, errorCode, null);
83   }
84
85   /**
86      Prints the message and the stack trace of the exception on
87      <code>System.err</code>.
88    */

89   public
90   void error(String JavaDoc message, Exception JavaDoc e, int errorCode, LoggingEvent event) {
91     if(firstTime) {
92       output.println(ERROR_PREFIX + message);
93       e.printStackTrace(output);
94       firstTime = false;
95     }
96   }
97
98
99   /**
100      Print a the error message passed as parameter on
101      <code>System.err</code>.
102   */

103   public
104   void error(String JavaDoc message) {
105     if(firstTime) {
106       output.println(ERROR_PREFIX + message);
107       firstTime = false;
108     }
109   }
110   
111   /**
112      Does not do anything.
113    */

114   public
115   void setAppender(Appender appender) {
116   }
117
118   /**
119      Does not do anything.
120    */

121   public
122   void setBackupAppender(Appender appender) {
123   }
124 }
125
Popular Tags