Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LOGMGR-211] Allow the console handler to act as an error manager for other handlers #208

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
jamezp marked this conversation as resolved.
Show resolved Hide resolved
}
};

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