1 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 ; 25 26 38 public class FallbackErrorHandler implements ErrorHandler { 39 40 41 Appender backup; 42 Appender primary; 43 Vector loggers; 44 45 public FallbackErrorHandler() { 46 } 47 48 49 53 public 54 void setLogger(Logger logger) { 55 LogLog.debug("FB: Adding logger [" + logger.getName() + "]."); 56 if(loggers == null) { 57 loggers = new Vector (); 58 } 59 loggers.addElement(logger); 60 } 61 62 63 66 public 67 void activateOptions() { 68 } 69 70 71 74 public 75 void error(String message, Exception e, int errorCode) { 76 error(message, e, errorCode, null); 77 } 78 79 83 public 84 void error(String message, Exception 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 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 106 public 107 void error(String message) { 108 } 113 114 117 public 118 void setAppender(Appender primary) { 119 LogLog.debug("FB: Setting primary appender to [" + primary.getName() + "]."); 120 this.primary = primary; 121 } 122 123 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 |