Add debug config options

This commit is contained in:
Vankka 2023-06-18 23:27:15 +03:00
parent 37476fd8f9
commit 11914b5d0e
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
3 changed files with 53 additions and 4 deletions

View File

@ -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<String, List<String>> additionalLevels = new HashMap<>();
}

View File

@ -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();
}

View File

@ -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<String> DISABLE_DEBUG_BY_DEFAULT = Collections.singletonList("Hikari");
private final Queue<LogEntry> 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();