Skip to content

Commit

Permalink
Merge pull request #208 from dmlloyd/console-errmgr
Browse files Browse the repository at this point in the history
[LOGMGR-211] Allow the console handler to act as an error manager for other handlers
  • Loading branch information
dmlloyd authored Nov 15, 2018
2 parents f014080 + e387b94 commit 5eb109b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 :
Expand Down

0 comments on commit 5eb109b

Please sign in to comment.