Improve JavaLoggerImpl

This commit is contained in:
Vankka 2024-12-26 00:39:50 +02:00
parent 54aadb9229
commit ef978cb04f
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB

View File

@ -30,10 +30,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.*;
public class JavaLoggerImpl implements Logger, LoggingBackend {
@ -66,17 +63,24 @@ public class JavaLoggerImpl implements Logger, LoggingBackend {
@Override
public void log(@Nullable String loggerName, @NotNull LogLevel level, @Nullable String message, @Nullable Throwable throwable) {
Level logLevel = LEVELS_REVERSE.get(level);
if (logLevel != null) {
List<String> contents = new ArrayList<>(2);
if (message != null) {
contents.add(message);
}
if (throwable != null) {
// Exceptions aren't always logged correctly by the logger itself
contents.add(getStackTrace(throwable));
}
logger.log(logLevel, String.join("\n", contents));
boolean anythingAdded = false;
StringBuilder stringBuilder = new StringBuilder(message != null ? message.length() : 0);
// Unknown level
if (logLevel == null) {
logLevel = Level.INFO;
stringBuilder.append("[").append(level.name()).append("] ");
anythingAdded = true;
}
if (message != null) {
stringBuilder.append(message);
anythingAdded = true;
}
String finalMessage = anythingAdded ? stringBuilder.toString() : null;
logger.log(logLevel, finalMessage, throwable);
}
@Override