From e387b943fb7860d2db16442b1ec7844bfa859820 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Wed, 14 Nov 2018 23:35:48 -0600 Subject: [PATCH] [LOGMGR-211] Allow the console handler to act as an error manager for other handlers --- .../logmanager/handlers/ConsoleHandler.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java b/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java index 41b79e8c..9b75e958 100644 --- a/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java +++ b/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java @@ -24,8 +24,11 @@ import java.io.PrintWriter; import java.io.Writer; +import java.util.logging.ErrorManager; import java.util.logging.Formatter; +import org.jboss.logmanager.ExtLogRecord; +import org.jboss.logmanager.Level; import org.jboss.logmanager.formatters.Formatters; /** @@ -56,6 +59,29 @@ public enum Target { private static final PrintWriter console; + private final ErrorManager localErrorManager = new ErrorManager() { + public void error(final String msg, final Exception ex, final int code) { + final ExtLogRecord record = new ExtLogRecord(Level.ERROR, "Failed to publish log record (%s[%d]): %s", ExtLogRecord.FormatStyle.PRINTF, getClass().getName()); + final String codeStr; + switch (code) { + case ErrorManager.GENERIC_FAILURE: codeStr = "GENERIC_FAILURE"; break; + case ErrorManager.WRITE_FAILURE: codeStr = "WRITE_FAILURE"; break; + case ErrorManager.FLUSH_FAILURE: codeStr = "FLUSH_FAILURE"; break; + case ErrorManager.CLOSE_FAILURE: codeStr = "CLOSE_FAILURE"; break; + case ErrorManager.OPEN_FAILURE: codeStr = "OPEN_FAILURE"; break; + case ErrorManager.FORMAT_FAILURE: codeStr = "FORMAT_FAILURE"; break; + default: codeStr = "Unknown Code"; break; + } + record.setParameters(new Object[] { + codeStr, + Integer.toString(code), + msg, + }); + record.setThrown(ex); + publish(record); + } + }; + static { final Console con = System.console(); console = con == null ? null : con.writer(); @@ -117,6 +143,25 @@ public void setTarget(Target target) { } } + public void setErrorManager(final ErrorManager em) { + if (em == localErrorManager) { + // ignore to avoid loops + super.setErrorManager(new ErrorManager()); + return; + } + super.setErrorManager(em); + } + + /** + * Get the local error manager. This is an error manager that will publish errors to this console handler. + * The console handler itself should not use this error manager. + * + * @return the local error manager + */ + public ErrorManager getLocalErrorManager() { + return localErrorManager; + } + private static OutputStream wrap(final OutputStream outputStream) { return outputStream == null ? null :