Improved error logging

This commit is contained in:
Reinhard Pointner 2016-04-12 06:19:45 +00:00
parent cf01fcf9e9
commit 9b60d6f206
2 changed files with 24 additions and 3 deletions

View File

@ -51,6 +51,20 @@ public final class Logging {
return () -> String.format(format, args);
}
public static Supplier<String> trace(Throwable t) {
return () -> {
StringBuilder s = new StringBuilder();
s.append(t.getClass().getSimpleName()).append(": ");
s.append(t.getMessage());
StackTraceElement[] trace = StackTraceUtils.sanitizeRootCause(t).getStackTrace();
if (trace != null && trace.length > 0) {
s.append(" at ").append(trace[0]);
}
return s.toString();
};
}
public static class ConsoleHandler extends Handler {
@Override

View File

@ -30,7 +30,6 @@ import java.util.logging.Logger;
import javax.script.Bindings;
import javax.script.SimpleBindings;
import org.codehaus.groovy.runtime.StackTraceUtils;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
import com.sun.jna.Platform;
@ -137,10 +136,18 @@ public abstract class ScriptShellBaseClass extends Script {
}
public void printException(Throwable t, boolean severe) {
Level level = severe ? Level.SEVERE : Level.WARNING;
// print full stacktrace in debug mode
if (debug.isLoggable(level)) {
debug.log(level, t, t::getMessage);
return;
}
if (severe) {
log.log(Level.SEVERE, String.format("%s: %s", t.getClass().getSimpleName(), t.getMessage()), StackTraceUtils.deepSanitize(t));
log.log(level, trace(t));
} else {
log.log(Level.WARNING, String.format("%s: %s", t.getClass().getSimpleName(), t.getMessage()));
log.log(level, t, t::getMessage);
}
}