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.async;
018
019import com.lmax.disruptor.ExceptionHandler;
020
021/**
022 * Default disruptor exception handler for errors that occur in the AsyncLogger background thread.
023 */
024public class AsyncLoggerDefaultExceptionHandler implements ExceptionHandler<RingBufferLogEvent> {
025
026    @Override
027    public void handleEventException(final Throwable throwable, final long sequence, final RingBufferLogEvent event) {
028        final StringBuilder sb = new StringBuilder(512);
029        sb.append("AsyncLogger error handling event seq=").append(sequence).append(", value='");
030        try {
031            sb.append(event);
032        } catch (final Exception ignored) {
033            sb.append("[ERROR calling ").append(event.getClass()).append(".toString(): ");
034            sb.append(ignored).append("]");
035        }
036        sb.append("':");
037        System.err.println(sb);
038        throwable.printStackTrace();
039    }
040
041    @Override
042    public void handleOnStartException(final Throwable throwable) {
043        System.err.println("AsyncLogger error starting:");
044        throwable.printStackTrace();
045    }
046
047    @Override
048    public void handleOnShutdownException(final Throwable throwable) {
049        System.err.println("AsyncLogger error shutting down:");
050        throwable.printStackTrace();
051    }
052}