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