Add filtering options for console appending

This commit is contained in:
Vankka 2023-09-07 18:19:31 +03:00
parent 367a51f04d
commit df1cf6274b
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
3 changed files with 76 additions and 33 deletions

View File

@ -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<String> 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<String> 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", "```"),

View File

@ -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<ConsoleConfig> console = new ArrayList<>();
public List<ConsoleConfig> 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();

View File

@ -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<LogMessage> 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<String> 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;