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
    27  /***
    28    *
    29    * The <code>FallbackErrorHandler</code> implements the ErrorHandler
    30    * interface such that a secondary appender may be specified.  This
    31    * secondary appender takes over if the primary appender fails for
    32    * whatever reason.
    33    *
    34    * <p>The error message is printed on <code>System.err</code>, and
    35    * logged in the new secondary appender.
    36    *
    37    * @author Ceki G&uuml;c&uuml;
    38    * */
    39  public class FallbackErrorHandler implements ErrorHandler {
    40
    41
    42    Appender backup;
    43    Appender primary;
    44    Vector loggers;
    45
    46    public FallbackErrorHandler() {
    47    }
    48
    49
    50    /***
    51       <em>Adds</em> the logger passed as parameter to the list of
    52       loggers that we need to search for in case of appender failure.
    53    */
    54    public
    55    void setLogger(Logger logger) {
    56      LogLog.debug("FB: Adding logger [" + logger.getName() + "].");
    57      if(loggers == null) {
    58        loggers = new Vector();
    59      }
    60      loggers.addElement(logger);
    61    }
    62
    63
    64    /***
    65       No options to activate.
    66    */
    67    public
    68    void activateOptions() {
    69    }
    70
    71
    72    /***
    73       Prints the message and the stack trace of the exception on
    74       <code>System.err</code>.  */
    75    public
    76    void error(String message, Exception e, int errorCode) {
    77      error(message, e, errorCode, null);
    78    }
    79
    80    /***
    81       Prints the message and the stack trace of the exception on
    82       <code>System.err</code>.
    83     */
    84    public
    85    void error(String message, Exception e, int errorCode, LoggingEvent event) {
    86      LogLog.debug("FB: The following error reported: " + message, e);
    87      LogLog.debug("FB: INITIATING FALLBACK PROCEDURE.");
    88      if (loggers != null) {
    89      	for(int i = 0; i < loggers.size(); i++) {
    90        		Logger l = (Logger) loggers.elementAt(i);
    91        		LogLog.debug("FB: Searching for ["+primary.getName()+"] in logger ["
    92  		   		+l.getName() + "].");
    93        		LogLog.debug("FB: Replacing ["+primary.getName()+"] by ["
    94  		   		+ backup.getName() + "] in logger ["+ l.getName() +"].");
    95        		l.removeAppender(primary);
    96        		LogLog.debug("FB: Adding appender ["+backup.getName()+"] to logger "
    97  		   		+  l.getName());
    98        		l.addAppender(backup);
    99          }
    100     }
    101   }
    102
    103
    104   /***
    105      Print a the error message passed as parameter on
    106      <code>System.err</code>.
    107   */
    108   public
    109   void error(String message) {
    110     //if(firstTime) {
    111     //LogLog.error(message);
    112     //firstTime = false;
    113     //}
    114   }
    115
    116   /***
    117      The appender to which this error handler is attached.
    118    */
    119   public
    120   void setAppender(Appender primary) {
    121     LogLog.debug("FB: Setting primary appender to [" + primary.getName() + "].");
    122     this.primary = primary;
    123   }
    124
    125   /***
    126      Set the backup appender.
    127    */
    128   public
    129   void setBackupAppender(Appender backup) {
    130     LogLog.debug("FB: Setting backup appender to [" + backup.getName() + "].");
    131     this.backup = backup;
    132   }
    133
    134 }