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 */
017package org.apache.logging.log4j.core.appender;
018
019import java.io.Serializable;
020import java.util.concurrent.TimeUnit;
021
022import org.apache.logging.log4j.Logger;
023import org.apache.logging.log4j.core.Appender;
024import org.apache.logging.log4j.core.ErrorHandler;
025import org.apache.logging.log4j.core.LogEvent;
026import org.apache.logging.log4j.status.StatusLogger;
027
028/**
029 *
030 */
031public class DefaultErrorHandler implements ErrorHandler, Serializable {
032
033    private static final long serialVersionUID = 1L;
034
035    private static final Logger LOGGER = StatusLogger.getLogger();
036
037    private static final int MAX_EXCEPTIONS = 3;
038
039    private static final long EXCEPTION_INTERVAL = TimeUnit.MINUTES.toNanos(5);
040
041    private int exceptionCount = 0;
042
043    private long lastException = System.nanoTime() - EXCEPTION_INTERVAL - 1;
044
045    private final Appender appender;
046
047    public DefaultErrorHandler(final Appender appender) {
048        this.appender = appender;
049    }
050
051
052    /**
053     * Handle an error with a message.
054     * @param msg The message.
055     */
056    @Override
057    public void error(final String msg) {
058        final long current = System.nanoTime();
059        if (current - lastException > EXCEPTION_INTERVAL || exceptionCount++ < MAX_EXCEPTIONS) {
060            LOGGER.error(msg);
061        }
062        lastException = current;
063    }
064
065    /**
066     * Handle an error with a message and an exception.
067     * @param msg The message.
068     * @param t The Throwable.
069     */
070    @Override
071    public void error(final String msg, final Throwable t) {
072        final long current = System.nanoTime();
073        if (current - lastException > EXCEPTION_INTERVAL || exceptionCount++ < MAX_EXCEPTIONS) {
074            LOGGER.error(msg, t);
075        }
076        lastException = current;
077        if (!appender.ignoreExceptions() && t != null && !(t instanceof AppenderLoggingException)) {
078            throw new AppenderLoggingException(msg, t);
079        }
080    }
081
082    /**
083     * Handle an error with a message, and exception and a logging event.
084     * @param msg The message.
085     * @param event The LogEvent.
086     * @param t The Throwable.
087     */
088    @Override
089    public void error(final String msg, final LogEvent event, final Throwable t) {
090        final long current = System.nanoTime();
091        if (current - lastException > EXCEPTION_INTERVAL || exceptionCount++ < MAX_EXCEPTIONS) {
092            LOGGER.error(msg, t);
093        }
094        lastException = current;
095        if (!appender.ignoreExceptions() && t != null && !(t instanceof AppenderLoggingException)) {
096            throw new AppenderLoggingException(msg, t);
097        }
098    }
099}