001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 
018package org.apache.log4j.varia;
019
020import  org.apache.log4j.spi.ErrorHandler;
021import  org.apache.log4j.spi.LoggingEvent;
022import  org.apache.log4j.Appender;
023import  org.apache.log4j.Logger;
024import  org.apache.log4j.helpers.LogLog;
025import java.util.Vector;
026import java.io.InterruptedIOException;
027
028/**
029  *
030  * The <code>FallbackErrorHandler</code> implements the ErrorHandler
031  * interface such that a secondary appender may be specified.  This
032  * secondary appender takes over if the primary appender fails for
033  * whatever reason.
034  *
035  * <p>The error message is printed on <code>System.err</code>, and
036  * logged in the new secondary appender.
037  *
038  * @author Ceki G&uuml;c&uuml;
039  * */
040public class FallbackErrorHandler implements ErrorHandler {
041
042
043  Appender backup;
044  Appender primary;
045  Vector loggers;
046
047  public FallbackErrorHandler() {
048  }
049  
050
051  /**
052     <em>Adds</em> the logger passed as parameter to the list of
053     loggers that we need to search for in case of appender failure.
054  */
055  public 
056  void setLogger(Logger logger) {
057    LogLog.debug("FB: Adding logger [" + logger.getName() + "].");
058    if(loggers == null) {
059      loggers = new Vector();
060    }
061    loggers.addElement(logger);
062  }
063
064
065  /**
066     No options to activate.
067  */
068  public 
069  void activateOptions() {
070  }
071
072
073  /**
074     Prints the message and the stack trace of the exception on
075     <code>System.err</code>.  */
076  public
077  void error(String message, Exception e, int errorCode) { 
078    error(message, e, errorCode, null);
079  }
080
081  /**
082     Prints the message and the stack trace of the exception on
083     <code>System.err</code>.
084   */
085  public
086  void error(String message, Exception e, int errorCode, LoggingEvent event) {
087    if (e instanceof InterruptedIOException) {
088        Thread.currentThread().interrupt();
089    }
090    LogLog.debug("FB: The following error reported: " + message, e);
091    LogLog.debug("FB: INITIATING FALLBACK PROCEDURE.");
092    if (loggers != null) {
093        for(int i = 0; i < loggers.size(); i++) {
094                Logger l = (Logger) loggers.elementAt(i);
095                LogLog.debug("FB: Searching for ["+primary.getName()+"] in logger ["
096                                +l.getName() + "].");
097                LogLog.debug("FB: Replacing ["+primary.getName()+"] by ["
098                                + backup.getName() + "] in logger ["+ l.getName() +"].");
099                l.removeAppender(primary);
100                LogLog.debug("FB: Adding appender ["+backup.getName()+"] to logger "
101                                +  l.getName());
102                l.addAppender(backup);
103        }
104    }    
105  }
106
107
108  /**
109     Print a the error message passed as parameter on
110     <code>System.err</code>.  
111  */
112  public 
113  void error(String message) {
114    //if(firstTime) {
115    //LogLog.error(message);
116    //firstTime = false;
117    //}
118  }
119  
120  /**
121     The appender to which this error handler is attached.
122   */
123  public
124  void setAppender(Appender primary) {
125    LogLog.debug("FB: Setting primary appender to [" + primary.getName() + "].");
126    this.primary = primary;
127  }
128
129  /**
130     Set the backup appender.
131   */
132  public
133  void setBackupAppender(Appender backup) {
134    LogLog.debug("FB: Setting backup appender to [" + backup.getName() + "].");
135    this.backup = backup;
136  }
137  
138}