From 1586fcb92733c44c8029b910d30a5117bdfc62b9 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 18 Jun 2023 23:36:15 +0300 Subject: [PATCH] Don't use dependencies that would be downloaded in loggers --- .../com/discordsrv/common/logging/Logger.java | 10 +++++++++ .../logging/backend/impl/JavaLoggerImpl.java | 20 +++++++++++------- .../logging/backend/impl/Log4JLoggerImpl.java | 21 ++++++++++++------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/logging/Logger.java b/common/src/main/java/com/discordsrv/common/logging/Logger.java index 09fea4bd..2d75e27e 100644 --- a/common/src/main/java/com/discordsrv/common/logging/Logger.java +++ b/common/src/main/java/com/discordsrv/common/logging/Logger.java @@ -21,6 +21,9 @@ package com.discordsrv.common.logging; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.PrintWriter; +import java.io.StringWriter; + public interface Logger { default void info(String message) { @@ -69,4 +72,11 @@ public interface Logger { void log(@Nullable String loggerName, @NotNull LogLevel logLevel, @Nullable String message, @Nullable Throwable throwable); + default String getStackTrace(Throwable throwable) { + final StringWriter stringWriter = new StringWriter(); + final PrintWriter printWriter = new PrintWriter(stringWriter, true); + throwable.printStackTrace(printWriter); + return stringWriter.getBuffer().toString(); + } + } diff --git a/common/src/main/java/com/discordsrv/common/logging/backend/impl/JavaLoggerImpl.java b/common/src/main/java/com/discordsrv/common/logging/backend/impl/JavaLoggerImpl.java index 369cb6c4..289b6527 100644 --- a/common/src/main/java/com/discordsrv/common/logging/backend/impl/JavaLoggerImpl.java +++ b/common/src/main/java/com/discordsrv/common/logging/backend/impl/JavaLoggerImpl.java @@ -23,8 +23,6 @@ import com.discordsrv.common.logging.LogLevel; import com.discordsrv.common.logging.Logger; import com.discordsrv.common.logging.backend.LogFilter; import com.discordsrv.common.logging.backend.LoggingBackend; -import org.apache.commons.collections4.bidimap.DualHashBidiMap; -import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -39,12 +37,18 @@ import java.util.logging.LogRecord; public class JavaLoggerImpl implements Logger, LoggingBackend { - private static final DualHashBidiMap LEVELS = new DualHashBidiMap<>(); + private static final Map LEVELS = new HashMap<>(); + private static final Map LEVELS_REVERSE = new HashMap<>(); + + private static void put(Level level, LogLevel logLevel) { + LEVELS.put(level, logLevel); + LEVELS_REVERSE.put(logLevel, level); + } static { - LEVELS.put(Level.INFO, LogLevel.INFO); - LEVELS.put(Level.WARNING, LogLevel.WARNING); - LEVELS.put(Level.SEVERE, LogLevel.ERROR); + put(Level.INFO, LogLevel.INFO); + put(Level.WARNING, LogLevel.WARNING); + put(Level.SEVERE, LogLevel.ERROR); } private final java.util.logging.Logger logger; @@ -61,7 +65,7 @@ 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.getKey(level); + Level logLevel = LEVELS_REVERSE.get(level); if (logLevel != null) { List contents = new ArrayList<>(2); if (message != null) { @@ -69,7 +73,7 @@ public class JavaLoggerImpl implements Logger, LoggingBackend { } if (throwable != null) { // Exceptions aren't always logged correctly by the logger itself - contents.add(ExceptionUtils.getStackTrace(throwable)); + contents.add(getStackTrace(throwable)); } logger.log(logLevel, String.join("\n", contents)); } diff --git a/common/src/main/java/com/discordsrv/common/logging/backend/impl/Log4JLoggerImpl.java b/common/src/main/java/com/discordsrv/common/logging/backend/impl/Log4JLoggerImpl.java index cf6be22b..5a942e2d 100644 --- a/common/src/main/java/com/discordsrv/common/logging/backend/impl/Log4JLoggerImpl.java +++ b/common/src/main/java/com/discordsrv/common/logging/backend/impl/Log4JLoggerImpl.java @@ -23,7 +23,6 @@ import com.discordsrv.common.logging.LogLevel; import com.discordsrv.common.logging.Logger; import com.discordsrv.common.logging.backend.LogFilter; import com.discordsrv.common.logging.backend.LoggingBackend; -import org.apache.commons.collections4.bidimap.DualHashBidiMap; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Marker; @@ -41,14 +40,20 @@ import java.util.Map; public class Log4JLoggerImpl implements Logger, LoggingBackend { - private static final DualHashBidiMap LEVELS = new DualHashBidiMap<>(); + private static final Map LEVELS = new HashMap<>(); + private static final Map LEVELS_REVERSE = new HashMap<>(); + + private static void put(Level level, LogLevel logLevel) { + LEVELS.put(level, logLevel); + LEVELS_REVERSE.put(logLevel, level); + } static { - LEVELS.put(Level.INFO, LogLevel.INFO); - LEVELS.put(Level.WARN, LogLevel.WARNING); - LEVELS.put(Level.ERROR, LogLevel.ERROR); - LEVELS.put(Level.DEBUG, LogLevel.DEBUG); - LEVELS.put(Level.TRACE, LogLevel.TRACE); + put(Level.INFO, LogLevel.INFO); + put(Level.WARN, LogLevel.WARNING); + put(Level.ERROR, LogLevel.ERROR); + put(Level.DEBUG, LogLevel.DEBUG); + put(Level.TRACE, LogLevel.TRACE); } private final org.apache.logging.log4j.Logger logger; @@ -65,7 +70,7 @@ public class Log4JLoggerImpl implements Logger, LoggingBackend { @Override public void log(@Nullable String loggerName, @NotNull LogLevel level, @Nullable String message, @Nullable Throwable throwable) { - Level logLevel = LEVELS.getKey(level); + Level logLevel = LEVELS_REVERSE.get(level); logger.log(logLevel, message, throwable); }