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.helpers;
019
020import  org.apache.log4j.spi.ErrorHandler;
021import  org.apache.log4j.spi.LoggingEvent;
022import  org.apache.log4j.Logger;
023import  org.apache.log4j.Appender;
024
025import java.io.InterruptedIOException;
026
027/**
028
029   The <code>OnlyOnceErrorHandler</code> implements log4j's default
030   error handling policy which consists of emitting a message for the
031   first error in an appender and ignoring all following errors.
032
033   <p>The error message is printed on <code>System.err</code>. 
034
035   <p>This policy aims at protecting an otherwise working application
036   from being flooded with error messages when logging fails.
037
038   @author Ceki G&uuml;lc&uuml;
039   @since 0.9.0 */
040public class OnlyOnceErrorHandler implements ErrorHandler {
041
042
043  final String WARN_PREFIX = "log4j warning: ";
044  final String ERROR_PREFIX = "log4j error: ";
045
046  boolean firstTime = true;
047
048
049  /**
050     Does not do anything.
051   */
052  public 
053  void setLogger(Logger logger) {
054  }
055
056
057  /**
058     No options to activate.
059  */
060  public 
061  void activateOptions() {
062  }
063
064
065  /**
066     Prints the message and the stack trace of the exception on
067     <code>System.err</code>.  */
068  public
069  void error(String message, Exception e, int errorCode) { 
070    error(message, e, errorCode, null);
071  }
072
073  /**
074     Prints the message and the stack trace of the exception on
075     <code>System.err</code>.
076   */
077  public
078  void error(String message, Exception e, int errorCode, LoggingEvent event) {
079    if (e instanceof InterruptedIOException || e instanceof InterruptedException) {
080        Thread.currentThread().interrupt();
081    }
082    if(firstTime) {
083      LogLog.error(message, e);
084      firstTime = false;
085    }
086  }
087
088
089  /**
090     Print a the error message passed as parameter on
091     <code>System.err</code>.  
092  */
093  public 
094  void error(String message) {
095    if(firstTime) {
096      LogLog.error(message);
097      firstTime = false;
098    }
099  }
100  
101  /**
102     Does not do anything.
103   */
104  public
105  void setAppender(Appender appender) {
106  }
107
108  /**
109     Does not do anything.
110   */
111  public
112  void setBackupAppender(Appender appender) {
113  }
114}