From df1cf6274bc381902b37b5939d55ead9abd8dce6 Mon Sep 17 00:00:00 2001 From: Vankka Date: Thu, 7 Sep 2023 18:19:31 +0300 Subject: [PATCH] Add filtering options for console appending --- .../common/config/main/ConsoleConfig.java | 70 ++++++++++++------- .../common/config/main/MainConfig.java | 2 +- .../common/console/SingleConsoleHandler.java | 37 +++++++--- 3 files changed, 76 insertions(+), 33 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/config/main/ConsoleConfig.java b/common/src/main/java/com/discordsrv/common/config/main/ConsoleConfig.java index 8f8e4934..adebaf6d 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/ConsoleConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/ConsoleConfig.java @@ -3,41 +3,63 @@ package com.discordsrv.common.config.main; import com.discordsrv.common.config.main.generic.DestinationConfig; import org.spongepowered.configurate.objectmapping.meta.Comment; -import java.util.Locale; +import java.util.*; public class ConsoleConfig { @Comment("The console channel or thread") public DestinationConfig.Single channel = new DestinationConfig.Single(); - @Comment("The format for log lines") - public String lineFormat = "[%log_time:'ccc HH:mm:ss zzz'%] [%log_level%] [%logger_name%] %message%"; + public Appender appender = new Appender(); - @Comment("The mode for the console output, available options are:\n" - + "- ansi: A colored ansi code block\n" - + "- log: A \"accesslog\" code block\n" - + "- diff: A \"diff\" code block highlighting warnings and errors with different colors\n" - + "- plain: Plain text code block\n" - + "- plain_content: Plain text") - public String outputMode = "ansi"; + public static class Appender { - public OutputMode getOutputMode() { - switch (outputMode.toLowerCase(Locale.ROOT)) { - default: - case "ansi": return OutputMode.ANSI; - case "log": return OutputMode.LOG; - case "diff": return OutputMode.DIFF; - case "plain": return OutputMode.PLAIN; - case "plain_content": return OutputMode.PLAIN_CONTENT; + @Comment("The format for log lines") + public String lineFormat = "[%log_time:'ccc HH:mm:ss zzz'%] [%log_level%] [%logger_name%] %message%"; + + @Comment("The mode for the console output, available options are:\n" + + "- ansi: A colored ansi code block\n" + + "- log: A \"accesslog\" code block\n" + + "- diff: A \"diff\" code block highlighting warnings and errors with different colors\n" + + "- plain: Plain text code block\n" + + "- plain_content: Plain text") + public String outputMode = "ansi"; + + public OutputMode getOutputMode() { + switch (outputMode.toLowerCase(Locale.ROOT)) { + default: + case "ansi": return OutputMode.ANSI; + case "log": return OutputMode.LOG; + case "diff": return OutputMode.DIFF; + case "plain": return OutputMode.PLAIN; + case "plain_content": return OutputMode.PLAIN_CONTENT; + } } + + @Comment("Avoids sending new messages by editing the most recent message until it reaches it's maximum length") + public boolean useEditing = true; + + @Comment("If console messages should be silent, not causing a notification") + public boolean silentMessages = true; + + @Comment("A list of log levels to whitelist or blacklist") + public Levels levels = new Levels(); + + public static class Levels { + public List levels = new ArrayList<>(Arrays.asList("DEBUG", "TRACE")); + public boolean blacklist = true; + } + + @Comment("A list of logger names to whitelist or blacklist, use \"NONE\" for log messages with no logger name") + public Loggers loggers = new Loggers(); + + public static class Loggers { + public List loggers = new ArrayList<>(Collections.singletonList("ExcludedLogger")); + public boolean blacklist = true; + } + } - @Comment("Avoids sending new messages by editing the most recent message until it reaches it's maximum length") - public boolean useEditing = true; - - @Comment("If console messages should be silent, not causing a notification") - public boolean silentMessages = true; - public enum OutputMode { ANSI("```ansi\n", "```"), LOG("```accesslog\n", "```"), 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 c0572687..c5132e38 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 @@ -83,7 +83,7 @@ public abstract class MainConfig implements Config { public DiscordCommandConfig discordCommand = new DiscordCommandConfig(); @Comment("Options for console channel(s) and/or thread(s)") - public List console = new ArrayList<>(); + public List console = new ArrayList<>(Collections.singleton(new ConsoleConfig())); @Comment("Configuration for the %discord_invite% placeholder. The below options will be attempted in the order they are in") public DiscordInviteConfig invite = new DiscordInviteConfig(); diff --git a/common/src/main/java/com/discordsrv/common/console/SingleConsoleHandler.java b/common/src/main/java/com/discordsrv/common/console/SingleConsoleHandler.java index dddf16ce..f8f0d4d7 100644 --- a/common/src/main/java/com/discordsrv/common/console/SingleConsoleHandler.java +++ b/common/src/main/java/com/discordsrv/common/console/SingleConsoleHandler.java @@ -8,12 +8,14 @@ import com.discordsrv.common.console.entry.LogEntry; import com.discordsrv.common.console.entry.LogMessage; import com.discordsrv.common.logging.LogLevel; import net.dv8tion.jda.api.entities.Message; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.Queue; -import java.util.concurrent.*; +import java.util.*; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; public class SingleConsoleHandler { @@ -35,7 +37,7 @@ public class SingleConsoleHandler { public SingleConsoleHandler(DiscordSRV discordSRV, ConsoleConfig config) { this.discordSRV = discordSRV; this.config = config; - this.messageCache = config.useEditing ? new ArrayList<>() : null; + this.messageCache = config.appender.useEditing ? new ArrayList<>() : null; this.queueProcessingFuture = discordSRV.scheduler().runAtFixedRate(this::processQueue, 2, TimeUnit.SECONDS); } @@ -52,11 +54,30 @@ public class SingleConsoleHandler { } private void processQueue() { - ConsoleConfig.OutputMode outputMode = config.getOutputMode(); + if (sendFuture != null && !sendFuture.isDone()) { + // Previous send still in progress. + return; + } + + ConsoleConfig.Appender appenderConfig = config.appender; + ConsoleConfig.OutputMode outputMode = appenderConfig.getOutputMode(); Queue currentBuffer = new LinkedBlockingQueue<>(); LogEntry entry; while ((entry = queue.poll()) != null) { + String level = entry.level().name(); + if (appenderConfig.levels.levels.contains(level) == appenderConfig.levels.blacklist) { + // Ignored level + continue; + } + + String loggerName = entry.loggerName(); + if (StringUtils.isEmpty(loggerName)) loggerName = "NONE"; + if (appenderConfig.loggers.loggers.contains(loggerName) == appenderConfig.loggers.blacklist) { + // Ignored logger + continue; + } + List messages = formatEntry(entry, outputMode); if (messages.size() == 1) { LogMessage message = new LogMessage(entry, messages.get(0)); @@ -106,7 +127,7 @@ public class SingleConsoleHandler { private void send(String message, boolean isFull, ConsoleConfig.OutputMode outputMode) { SendableDiscordMessage sendableMessage = SendableDiscordMessage.builder() .setContent(outputMode.prefix() + message + outputMode.suffix()) - .setSuppressedNotifications(config.silentMessages) + .setSuppressedNotifications(config.appender.silentMessages) .build(); synchronized (sendLock) { @@ -148,7 +169,7 @@ public class SingleConsoleHandler { int blockLength = outputMode.blockLength(); int maximumPart = MESSAGE_MAX_LENGTH - blockLength - "\n".length(); - String message = discordSRV.placeholderService().replacePlaceholders(config.lineFormat, entry) + "\n"; + String message = discordSRV.placeholderService().replacePlaceholders(config.appender.lineFormat, entry) + "\n"; if (outputMode == ConsoleConfig.OutputMode.DIFF) { message = getLogLevelDiffCharacter(entry.level()) + message;