KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > varia > FallbackErrorHandler


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.varia;
18
19 import org.apache.log4j.spi.ErrorHandler;
20 import org.apache.log4j.spi.LoggingEvent;
21 import org.apache.log4j.Appender;
22 import org.apache.log4j.Logger;
23 import org.apache.log4j.helpers.LogLog;
24 import java.util.Vector JavaDoc;
25  
26 /**
27   *
28   * The <code>FallbackErrorHandler</code> implements the ErrorHandler
29   * interface such that a secondary appender may be specified. This
30   * secondary appender takes over if the primary appender fails for
31   * whatever reason.
32   *
33   * <p>The error message is printed on <code>System.err</code>, and
34   * logged in the new secondary appender.
35   *
36   * @author Ceki G&uuml;c&uuml;
37   * */

38 public class FallbackErrorHandler implements ErrorHandler {
39
40
41   Appender backup;
42   Appender primary;
43   Vector JavaDoc loggers;
44
45   public FallbackErrorHandler() {
46   }
47   
48
49   /**
50      <em>Adds</em> the logger passed as parameter to the list of
51      loggers that we need to search for in case of appender failure.
52   */

53   public
54   void setLogger(Logger logger) {
55     LogLog.debug("FB: Adding logger [" + logger.getName() + "].");
56     if(loggers == null) {
57       loggers = new Vector JavaDoc();
58     }
59     loggers.addElement(logger);
60   }
61
62
63   /**
64      No options to activate.
65   */

66   public
67   void activateOptions() {
68   }
69
70
71   /**
72      Prints the message and the stack trace of the exception on
73      <code>System.err</code>. */

74   public
75   void error(String JavaDoc message, Exception JavaDoc e, int errorCode) {
76     error(message, e, errorCode, null);
77   }
78
79   /**
80      Prints the message and the stack trace of the exception on
81      <code>System.err</code>.
82    */

83   public
84   void error(String JavaDoc message, Exception JavaDoc e, int errorCode, LoggingEvent event) {
85     LogLog.debug("FB: The following error reported: " + message, e);
86     LogLog.debug("FB: INITIATING FALLBACK PROCEDURE.");
87     for(int i = 0; i < loggers.size(); i++) {
88       Logger l = (Logger) loggers.elementAt(i);
89       LogLog.debug("FB: Searching for ["+primary.getName()+"] in logger ["
90            +l.getName() + "].");
91       //if(l.isAttached(primary)) {
92
LogLog.debug("FB: Replacing ["+primary.getName()+"] by ["
93            + backup.getName() + "] in logger ["+ l.getName() +"].");
94       l.removeAppender(primary);
95       LogLog.debug("FB: Adding appender ["+backup.getName()+"] to logger "
96            + l.getName());
97       l.addAppender(backup);
98     }
99   }
100
101
102   /**
103      Print a the error message passed as parameter on
104      <code>System.err</code>.
105   */

106   public
107   void error(String JavaDoc message) {
108     //if(firstTime) {
109
//LogLog.error(message);
110
//firstTime = false;
111
//}
112
}
113   
114   /**
115      The appender to which this error handler is attached.
116    */

117   public
118   void setAppender(Appender primary) {
119     LogLog.debug("FB: Setting primary appender to [" + primary.getName() + "].");
120     this.primary = primary;
121   }
122
123   /**
124      Set the backup appender.
125    */

126   public
127   void setBackupAppender(Appender backup) {
128     LogLog.debug("FB: Setting backup appender to [" + backup.getName() + "].");
129     this.backup = backup;
130   }
131   
132 }
133
Popular Tags