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.helpers;
    19
    20  import  org.apache.log4j.spi.ErrorHandler;
    21  import  org.apache.log4j.spi.LoggingEvent;
    22  import  org.apache.log4j.Logger;
    23  import  org.apache.log4j.Appender;
    24
    25  import java.io.InterruptedIOException;
    26
    27  /**
    28
    29     The <code>OnlyOnceErrorHandler</code> implements log4j's default
    30     error handling policy which consists of emitting a message for the
    31     first error in an appender and ignoring all following errors.
    32
    33     <p>The error message is printed on <code>System.err</code>.
    34
    35     <p>This policy aims at protecting an otherwise working application
    36     from being flooded with error messages when logging fails.
    37
    38     @author Ceki G&uuml;lc&uuml;
    39     @since 0.9.0 */
    40  public class OnlyOnceErrorHandler implements ErrorHandler {
    41
    42
    43    final String WARN_PREFIX = "log4j warning: ";
    44    final String ERROR_PREFIX = "log4j error: ";
    45
    46    boolean firstTime = true;
    47
    48
    49    /**
    50       Does not do anything.
    51     */
    52    public
    53    void setLogger(Logger logger) {
    54    }
    55
    56
    57    /**
    58       No options to activate.
    59    */
    60    public
    61    void activateOptions() {
    62    }
    63
    64
    65    /**
    66       Prints the message and the stack trace of the exception on
    67       <code>System.err</code>.  */
    68    public
    69    void error(String message, Exception e, int errorCode) {
    70      error(message, e, errorCode, null);
    71    }
    72
    73    /**
    74       Prints the message and the stack trace of the exception on
    75       <code>System.err</code>.
    76     */
    77    public
    78    void error(String message, Exception e, int errorCode, LoggingEvent event) {
    79      if (e instanceof InterruptedIOException || e instanceof InterruptedException) {
    80          Thread.currentThread().interrupt();
    81      }
    82      if(firstTime) {
    83        LogLog.error(message, e);
    84        firstTime = false;
    85      }
    86    }
    87
    88
    89    /**
    90       Print a the error message passed as parameter on
    91       <code>System.err</code>.
    92    */
    93    public
    94    void error(String message) {
    95      if(firstTime) {
    96        LogLog.error(message);
    97        firstTime = false;
    98      }
    99    }
    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 }