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     */
017    package org.apache.logging.log4j.core.appender;
018    
019    import org.apache.logging.log4j.Logger;
020    import org.apache.logging.log4j.core.Appender;
021    import org.apache.logging.log4j.core.ErrorHandler;
022    import org.apache.logging.log4j.core.LogEvent;
023    import org.apache.logging.log4j.status.StatusLogger;
024    
025    /**
026     *
027     */
028    public 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(Appender appender) {
043            this.appender = appender;
044        }
045    
046    
047        /**
048         * Handle an error with a message.
049         * @param msg The message.
050         */
051        public void error(String msg) {
052            long current = System.currentTimeMillis();
053            if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
054                LOGGER.error(msg);
055            }
056            lastException = current;
057        }
058    
059        /**
060         * Handle an error with a message and an exception.
061         * @param msg The message.
062         * @param t The Throwable.
063         */
064        public void error(String msg, Throwable t) {
065            long current = System.currentTimeMillis();
066            if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
067                LOGGER.error(msg, t);
068            }
069            lastException = current;
070            if (!appender.isExceptionSuppressed() && t != null) {
071                throw new AppenderRuntimeException(msg, t);
072            }
073        }
074    
075        /**
076         * Handle an error with a message, and exception and a logging event.
077         * @param msg The message.
078         * @param event The LogEvent.
079         * @param t The Throwable.
080         */
081        public void error(String msg, LogEvent event, Throwable t) {
082            long current = System.currentTimeMillis();
083            if (lastException + EXCEPTION_INTERVAL < current || exceptionCount++ < MAX_EXCEPTIONS) {
084                LOGGER.error(msg, t);
085            }
086            lastException = current;
087            if (!appender.isExceptionSuppressed() && t != null) {
088                throw new AppenderRuntimeException(msg, t);
089            }
090        }
091    }