1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.logging.log4j.core.async;
18
19 import com.lmax.disruptor.ExceptionHandler;
20
21 /**
22 * Default disruptor exception handler for errors that occur in the AsyncLogger background thread.
23 */
24 abstract class AbstractAsyncExceptionHandler<T> implements ExceptionHandler<T> {
25
26 @Override
27 public void handleEventException(final Throwable throwable, final long sequence, final T event) {
28 try {
29 // Careful to avoid allocation in case of memory pressure.
30 // Sacrifice performance for safety by writing directly
31 // rather than using a buffer.
32 System.err.print("AsyncLogger error handling event seq=");
33 System.err.print(sequence);
34 System.err.print(", value='");
35 try {
36 System.err.print(event);
37 } catch (final Throwable t) {
38 System.err.print("ERROR calling toString() on ");
39 System.err.print(event.getClass().getName());
40 System.err.print(": ");
41 System.err.print(t.getClass().getName());
42 System.err.print(": ");
43 System.err.print(t.getMessage());
44 }
45 System.err.print("': ");
46 System.err.print(throwable.getClass().getName());
47 System.err.print(": ");
48 System.err.println(throwable.getMessage());
49 // Attempt to print the full stack trace, which may fail if we're already
50 // OOMing We've already provided sufficient information at this point.
51 throwable.printStackTrace();
52 } catch (final Throwable ignored) {
53 // LOG4J2-2333: Not much we can do here without risking an OOM.
54 // Throwing an error here may kill the background thread.
55 }
56 }
57
58 @Override
59 public void handleOnStartException(final Throwable throwable) {
60 System.err.println("AsyncLogger error starting:");
61 throwable.printStackTrace();
62 }
63
64 @Override
65 public void handleOnShutdownException(final Throwable throwable) {
66 System.err.println("AsyncLogger error shutting down:");
67 throwable.printStackTrace();
68 }
69 }