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