diff --git a/source/net/filebot/Logging.java b/source/net/filebot/Logging.java index afbcd983..f58f5fd0 100644 --- a/source/net/filebot/Logging.java +++ b/source/net/filebot/Logging.java @@ -51,6 +51,20 @@ public final class Logging { return () -> String.format(format, args); } + public static Supplier 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 diff --git a/source/net/filebot/cli/ScriptShellBaseClass.java b/source/net/filebot/cli/ScriptShellBaseClass.java index 94edd7a9..69ed16c2 100644 --- a/source/net/filebot/cli/ScriptShellBaseClass.java +++ b/source/net/filebot/cli/ScriptShellBaseClass.java @@ -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); } }