From bc6c0ab785a50b01442914da479a6e9dd4d3e5f2 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Wed, 17 Jun 2020 09:31:29 -0500 Subject: [PATCH] Handle publication errors properly --- .../java/org/jboss/logmanager/LoggerNode.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/jboss/logmanager/LoggerNode.java b/core/src/main/java/org/jboss/logmanager/LoggerNode.java index a80f8e5d..c3b7c3c4 100644 --- a/core/src/main/java/org/jboss/logmanager/LoggerNode.java +++ b/core/src/main/java/org/jboss/logmanager/LoggerNode.java @@ -24,6 +24,9 @@ import org.wildfly.common.ref.Reaper; import org.wildfly.common.ref.Reference; +import java.lang.reflect.UndeclaredThrowableException; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; @@ -31,6 +34,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; +import java.util.logging.ErrorManager; import java.util.logging.Filter; import java.util.logging.Handler; import java.util.logging.Level; @@ -46,6 +50,7 @@ public void reap(Reference reference) { reference.getAttachment().activeLoggers.remove(reference); } }; + private static final StackTraceElement[] EMPTY_STACK = new StackTraceElement[0]; /** * The log context. @@ -386,7 +391,26 @@ void publish(final ExtLogRecord record) { } catch (VirtualMachineError e) { throw e; } catch (Throwable t) { - // todo - error handler + ErrorManager errorManager = AccessController.doPrivileged(new PrivilegedAction() { + @Override + public ErrorManager run() { + return handler.getErrorManager(); + } + }); + if (errorManager != null) { + Exception e; + if (t instanceof Exception) { + e = (Exception) t; + } else { + e = new UndeclaredThrowableException(t); + e.setStackTrace(EMPTY_STACK); + } + try { + errorManager.error("Handler publication threw an exception", e, ErrorManager.WRITE_FAILURE); + } catch (Throwable t2) { + StandardOutputStreams.printError(t2, "Handler.reportError caught an exception"); + } + } } if (useParentHandlers) { final LoggerNode parent = this.parent;