From 11914b5d0ea16590fcac7e93e6f988238df75170 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 18 Jun 2023 23:27:15 +0300 Subject: [PATCH] Add debug config options --- .../common/config/main/DebugConfig.java | 19 +++++++++++ .../common/config/main/MainConfig.java | 4 +++ .../common/logging/impl/DiscordSRVLogger.java | 34 ++++++++++++++++--- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/com/discordsrv/common/config/main/DebugConfig.java diff --git a/common/src/main/java/com/discordsrv/common/config/main/DebugConfig.java b/common/src/main/java/com/discordsrv/common/config/main/DebugConfig.java new file mode 100644 index 00000000..4716fafc --- /dev/null +++ b/common/src/main/java/com/discordsrv/common/config/main/DebugConfig.java @@ -0,0 +1,19 @@ +package com.discordsrv.common.config.main; + +import org.spongepowered.configurate.objectmapping.ConfigSerializable; +import org.spongepowered.configurate.objectmapping.meta.Comment; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@ConfigSerializable +public class DebugConfig { + + @Comment("If debug messages should be logged into the config") + public boolean logToConsole = false; + + @Comment("Additional levels to log\nExample value: {\"AWARD_LISTENER\":[\"TRACE\"]}") + public Map> additionalLevels = new HashMap<>(); + +} diff --git a/common/src/main/java/com/discordsrv/common/config/main/MainConfig.java b/common/src/main/java/com/discordsrv/common/config/main/MainConfig.java index a0d9b9e8..aa4cbc38 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/MainConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/MainConfig.java @@ -84,4 +84,8 @@ public abstract class MainConfig implements Config { @Order(1000) public MemberCachingConfig memberCaching = new MemberCachingConfig(); + + @Order(5000) + @Comment("Options for diagnosing DiscordSRV, you do not need to touch these options during normal operation") + public DebugConfig debug = new DebugConfig(); } diff --git a/common/src/main/java/com/discordsrv/common/logging/impl/DiscordSRVLogger.java b/common/src/main/java/com/discordsrv/common/logging/impl/DiscordSRVLogger.java index 1385212b..48488d5f 100644 --- a/common/src/main/java/com/discordsrv/common/logging/impl/DiscordSRVLogger.java +++ b/common/src/main/java/com/discordsrv/common/logging/impl/DiscordSRVLogger.java @@ -19,6 +19,8 @@ package com.discordsrv.common.logging.impl; import com.discordsrv.common.DiscordSRV; +import com.discordsrv.common.config.main.DebugConfig; +import com.discordsrv.common.config.main.MainConfig; import com.discordsrv.common.logging.LogLevel; import com.discordsrv.common.logging.Logger; import net.dv8tion.jda.api.Permission; @@ -36,6 +38,7 @@ import java.nio.file.StandardOpenOption; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; @@ -47,6 +50,8 @@ public class DiscordSRVLogger implements Logger { private static final String LOG_LINE_FORMAT = "[%s] [%s] %s"; private static final String LOG_FILE_NAME_FORMAT = "%s-%s.log"; + private static final List DISABLE_DEBUG_BY_DEFAULT = Collections.singletonList("Hikari"); + private final Queue linesToAdd = new ConcurrentLinkedQueue<>(); private final DiscordSRV discordSRV; @@ -123,13 +128,34 @@ public class DiscordSRVLogger implements Logger { } private void doLog(String loggerName, LogLevel logLevel, String message, Throwable throwable) { - if (logLevel != LogLevel.DEBUG && logLevel != LogLevel.TRACE) { - discordSRV.platformLogger().log(null, logLevel, message, throwable); + MainConfig config = discordSRV.config(); + DebugConfig debugConfig = config != null ? config.debug : null; + + if (logLevel == LogLevel.TRACE || (loggerName != null && logLevel == LogLevel.DEBUG && DISABLE_DEBUG_BY_DEFAULT.contains(loggerName))) { + if (loggerName == null + || debugConfig == null + || debugConfig.additionalLevels == null + || !debugConfig.additionalLevels.getOrDefault(loggerName, Collections.emptyList()).contains(logLevel.name())) { + return; + } + } + + boolean debugOrTrace = logLevel == LogLevel.DEBUG || logLevel == LogLevel.TRACE; + boolean logToConsole = debugConfig != null && debugConfig.logToConsole; + + if (!debugOrTrace || logToConsole) { + String consoleMessage = message; + LogLevel consoleLevel = logLevel; + if (debugOrTrace) { + // Normally DEBUG/TRACE logging isn't enabled, so we convert it to INFO and add the level + consoleMessage = "[" + logLevel.name() + "]" + (message != null ? " " + message : ""); + consoleLevel = LogLevel.INFO; + } + discordSRV.platformLogger().log(null, consoleLevel, consoleMessage, throwable); } - // TODO: handle trace & hikari Path debugLog = debugLogs.isEmpty() ? null : debugLogs.get(0); - if (debugLog == null || logLevel == LogLevel.TRACE/* || loggerName.equals("Hikari")*/) { + if (debugLog == null) { return; } long time = System.currentTimeMillis();