View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.varia;
    19
    20  import  org.apache.log4j.spi.ErrorHandler;
    21  import  org.apache.log4j.spi.LoggingEvent;
    22  import  org.apache.log4j.Appender;
    23  import  org.apache.log4j.Logger;
    24  import  org.apache.log4j.helpers.LogLog;
    25  import java.util.Vector;
    26  import java.io.InterruptedIOException;
    27
    28  /**
    29    *
    30    * The <code>FallbackErrorHandler</code> implements the ErrorHandler
    31    * interface such that a secondary appender may be specified.  This
    32    * secondary appender takes over if the primary appender fails for
    33    * whatever reason.
    34    *
    35    * <p>The error message is printed on <code>System.err</code>, and
    36    * logged in the new secondary appender.
    37    *
    38    * @author Ceki G&uuml;c&uuml;
    39    * */
    40  public class FallbackErrorHandler implements ErrorHandler {
    41
    42
    43    Appender backup;
    44    Appender primary;
    45    Vector loggers;
    46
    47    public FallbackErrorHandler() {
    48    }
    49
    50
    51    /**
    52       <em>Adds</em> the logger passed as parameter to the list of
    53       loggers that we need to search for in case of appender failure.
    54    */
    55    public
    56    void setLogger(Logger logger) {
    57      LogLog.debug("FB: Adding logger [" + logger.getName() + "].");
    58      if(loggers == null) {
    59        loggers = new Vector();
    60      }
    61      loggers.addElement(logger);
    62    }
    63
    64
    65    /**
    66       No options to activate.
    67    */
    68    public
    69    void activateOptions() {
    70    }
    71
    72
    73    /**
    74       Prints the message and the stack trace of the exception on
    75       <code>System.err</code>.  */
    76    public
    77    void error(String message, Exception e, int errorCode) {
    78      error(message, e, errorCode, null);
    79    }
    80
    81    /**
    82       Prints the message and the stack trace of the exception on
    83       <code>System.err</code>.
    84     */
    85    public
    86    void error(String message, Exception e, int errorCode, LoggingEvent event) {
    87      if (e instanceof InterruptedIOException) {
    88          Thread.currentThread().interrupt();
    89      }
    90      LogLog.debug("FB: The following error reported: " + message, e);
    91      LogLog.debug("FB: INITIATING FALLBACK PROCEDURE.");
    92      if (loggers != null) {
    93      	for(int i = 0; i < loggers.size(); i++) {
    94        		Logger l = (Logger) loggers.elementAt(i);
    95        		LogLog.debug("FB: Searching for ["+primary.getName()+"] in logger ["
    96  		   		+l.getName() + "].");
    97        		LogLog.debug("FB: Replacing ["+primary.getName()+"] by ["
    98  		   		+ backup.getName() + "] in logger ["+ l.getName() +"].");
    99        		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 }