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