Improved throwable logging for two custom logger

This commit is contained in:
TomTheFurry
2022-05-01 13:14:29 +08:00
parent e116b891a8
commit be7a077367
2 changed files with 37 additions and 5 deletions
@@ -52,6 +52,21 @@ public class ConfigBasedLogger {
this.logger = logger;
loggers.add(new WeakReference<>(this));
}
private String _throwableToDetailString(Throwable t) {
StringBuilder sb = new StringBuilder();
sb.append(t.toString()).append('\n');
StackTraceElement[] stacks = t.getStackTrace();
if (stacks==null || stacks.length == 0)
return sb.append(" at {Stack trace unavailable}").toString();
for (StackTraceElement stack : stacks) {
sb.append(" at ").append(stack.toString()).append("\n");
}
return sb.toString();
}
public void update() {
mode = getter.get();
}
@@ -69,7 +84,8 @@ public class ConfigBasedLogger {
}
if (mode.levelForChat.isLessSpecificThan(level)) {
if (param.length > 0 && param[param.length-1] instanceof Throwable)
ClientApi.logToChat(level, msgStr + "\nat\n" + Arrays.toString(((Throwable) param[param.length - 1]).getStackTrace()));
ClientApi.logToChat(level, msgStr + "\n" +
_throwableToDetailString(((Throwable) param[param.length - 1])));
else ClientApi.logToChat(level, msgStr);
}
}
@@ -64,6 +64,20 @@ public class ConfigBasedSpamLogger {
mode = getter.get();
}
private String _throwableToDetailString(Throwable t) {
StringBuilder sb = new StringBuilder();
sb.append(t.toString()).append('\n');
StackTraceElement[] stacks = t.getStackTrace();
if (stacks==null || stacks.length == 0)
return sb.append(" at {Stack trace unavailable}").toString();
for (StackTraceElement stack : stacks) {
sb.append(" at ").append(stack.toString()).append("\n");
}
return sb.toString();
}
public void log(Level level, String str, Object... param) {
if (logTries.get() >= maxLogCount) return;
@@ -76,8 +90,9 @@ public class ConfigBasedSpamLogger {
else logger.log(logLevel, msgStr);
}
if (mode.levelForChat.isLessSpecificThan(level)) {
if (param.length > 0 && param[param.length - 1] instanceof Throwable)
ClientApi.logToChat(level, msgStr + "\nat\n" + Arrays.toString(((Throwable) param[param.length - 1]).getStackTrace()));
if (param.length > 0 && param[param.length-1] instanceof Throwable)
ClientApi.logToChat(level, msgStr + "\n" +
_throwableToDetailString(((Throwable) param[param.length - 1])));
else ClientApi.logToChat(level, msgStr);
}
}
@@ -114,8 +129,9 @@ public class ConfigBasedSpamLogger {
else logger.log(logLevel, msgStr);
}
if (mode.levelForChat.isLessSpecificThan(level)) {
if (param.length > 0 && param[param.length - 1] instanceof Throwable)
ClientApi.logToChat(level, msgStr + "\nat\n" + Arrays.toString(((Throwable) param[param.length - 1]).getStackTrace()));
if (param.length > 0 && param[param.length-1] instanceof Throwable)
ClientApi.logToChat(level, msgStr + "\n" +
_throwableToDetailString(((Throwable) param[param.length - 1])));
else ClientApi.logToChat(level, msgStr);
}
}