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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.varia;
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;
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
35 * <p>The error message is printed on <code>System.err</code>, and
36 * logged in the new secondary appender.
38 * @author Ceki Gücü
40 public class FallbackErrorHandler implements ErrorHandler {
47 public FallbackErrorHandler() {
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.
56 void setLogger(Logger logger) {
57 LogLog.debug("FB: Adding logger [" + logger.getName() + "].");
59 loggers = new Vector();
61 loggers.addElement(logger);
66 No options to activate.
69 void activateOptions() {
74 Prints the message and the stack trace of the exception on
75 <code>System.err</code>. */
77 void error(String message, Exception e, int errorCode) {
78 error(message, e, errorCode, null);
82 Prints the message and the stack trace of the exception on
83 <code>System.err</code>.
86 void error(String message, Exception e, int errorCode, LoggingEvent event) {
87 if (e instanceof InterruptedIOException) {
88 Thread.currentThread().interrupt();
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 ["
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 "
102 l.addAppender(backup);
109 Print a the error message passed as parameter on
110 <code>System.err</code>.
113 void error(String message) {
115 //LogLog.error(message);
121 The appender to which this error handler is attached.
124 void setAppender(Appender primary) {
125 LogLog.debug("FB: Setting primary appender to [" + primary.getName() + "].");
126 this.primary = primary;
130 Set the backup appender.
133 void setBackupAppender(Appender backup) {
134 LogLog.debug("FB: Setting backup appender to [" + backup.getName() + "].");
135 this.backup = backup;