From 0d803c6f448098b1b15905c6265a142a3ba36666 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Wed, 21 Sep 2022 15:27:58 +0200 Subject: [PATCH 1/8] Draft of discord log forwarding --- application/config.json.template | 3 +- .../org/togetherjava/tjbot/Application.java | 2 + .../org/togetherjava/tjbot/config/Config.java | 15 +++- .../logging/discord/DiscordLogAppender.java | 88 +++++++++++++++++++ .../logging/discord/DiscordLogMessage.java | 34 +++++++ .../discord/DiscordLogMessageEmbed.java | 10 +++ .../discord/DiscordLogMessageEmbedAuthor.java | 9 ++ .../tjbot/logging/discord/DiscordLogging.java | 37 ++++++++ 8 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogAppender.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessage.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbed.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbedAuthor.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java diff --git a/application/config.json.template b/application/config.json.template index 51faf35082..db14a383d0 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -83,5 +83,6 @@ "wsc", "wsf", "wsh" - ] + ], + "logChannelWebhook": "" } diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index 71128aef44..f2af6c959a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -12,6 +12,7 @@ import org.togetherjava.tjbot.commands.system.BotCore; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; +import org.togetherjava.tjbot.logging.discord.DiscordLogging; import java.io.IOException; import java.nio.file.Files; @@ -57,6 +58,7 @@ public static void main(final String[] args) { Thread.setDefaultUncaughtExceptionHandler(Application::onUncaughtException); Runtime.getRuntime().addShutdownHook(new Thread(Application::onShutdown)); + DiscordLogging.appendDiscordLogAppender(config.getLogChannelWebhook()); runBot(config); } diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index b2ac4062b9..9aa57ba3d2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -31,8 +31,8 @@ public final class Config { private final String wolframAlphaAppId; private final HelpSystemConfig helpSystem; private final List blacklistedFileExtension; - private final String mediaOnlyChannelPattern; + private final String logChannelWebhook; @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) @@ -59,7 +59,8 @@ private Config(@JsonProperty(value = "token", required = true) String token, @JsonProperty(value = "mediaOnlyChannelPattern", required = true) String mediaOnlyChannelPattern, @JsonProperty(value = "blacklistedFileExtension", - required = true) List blacklistedFileExtension) { + required = true) List blacklistedFileExtension, + @JsonProperty(value = "logChannelWebhook", required = true) String logChannelWebhook) { this.token = Objects.requireNonNull(token); this.gistApiKey = Objects.requireNonNull(gistApiKey); this.databasePath = Objects.requireNonNull(databasePath); @@ -77,6 +78,7 @@ private Config(@JsonProperty(value = "token", required = true) String token, this.helpSystem = Objects.requireNonNull(helpSystem); this.mediaOnlyChannelPattern = Objects.requireNonNull(mediaOnlyChannelPattern); this.blacklistedFileExtension = Objects.requireNonNull(blacklistedFileExtension); + this.logChannelWebhook = Objects.requireNonNull(logChannelWebhook); } /** @@ -250,4 +252,13 @@ public String getMediaOnlyChannelPattern() { public List getBlacklistedFileExtensions() { return Collections.unmodifiableList(blacklistedFileExtension); } + + /** + * The Discord channel webhook for posting log messages. + * + * @return the webhook URL + */ + public String getLogChannelWebhook() { + return logChannelWebhook; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogAppender.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogAppender.java new file mode 100644 index 0000000000..e85a4a74e3 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogAppender.java @@ -0,0 +1,88 @@ +package org.togetherjava.tjbot.logging.discord; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.logging.log4j.core.*; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.Property; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; + +import java.io.Serializable; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Objects; + +@Plugin(name = "Discord", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE) +final class DiscordLogAppender extends AbstractAppender { + + private static final Property[] NO_PROPERTIES = {}; + private static final HttpClient CLIENT = HttpClient.newHttpClient(); + private static final ObjectMapper JSON = new ObjectMapper(); + + private final URI webhook; + + private DiscordLogAppender(String name, Filter filter, StringLayout layout, + boolean ignoreExceptions, URI webhook) { + super(name, filter, layout, ignoreExceptions, NO_PROPERTIES); + + this.webhook = webhook; + } + + @Override + public void append(LogEvent event) { + DiscordLogMessage message = DiscordLogMessage.ofEvent(event); + String messageRaw; + try { + messageRaw = JSON.writeValueAsString(message); + } catch (JsonProcessingException e) { + // FIXME Some error reporting + return; + } + + HttpRequest request = HttpRequest.newBuilder(webhook) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(messageRaw)) + .build(); + + // Fire and forget + CLIENT.sendAsync(request, HttpResponse.BodyHandlers.discarding()); + // FIXME 429 Rate limiting, Retry-After + } + + @PluginBuilderFactory + static DiscordLogAppenderBuilder newBuilder() { + return new DiscordLogAppenderBuilder(); + } + + static final class DiscordLogAppenderBuilder + extends AbstractAppender.Builder + implements org.apache.logging.log4j.core.util.Builder { + + @PluginBuilderAttribute + @Required + private URI webhook; + + public DiscordLogAppenderBuilder setWebhook(URI webhook) { + this.webhook = webhook; + return asBuilder(); + } + + @Override + public DiscordLogAppender build() { + Layout layout = getOrCreateLayout(); + if (!(layout instanceof StringLayout)) { + throw new IllegalArgumentException("Layout must be a StringLayout"); + } + + String name = Objects.requireNonNull(getName()); + + return new DiscordLogAppender(name, getFilter(), (StringLayout) layout, + isIgnoreExceptions(), webhook); + } + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessage.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessage.java new file mode 100644 index 0000000000..fa349d411a --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessage.java @@ -0,0 +1,34 @@ +package org.togetherjava.tjbot.logging.discord; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.LogEvent; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * The Jackson representation of Discords Webhook messages: API + * Documentation + */ +record DiscordLogMessage(List embeds) { + private static final Map LEVEL_TO_AMBIENT_COLOR = + Map.of(Level.TRACE, 0x00B362, Level.DEBUG, 0x00A5CE, Level.INFO, 0xAC59FF, Level.WARN, + 0xDFDF00, Level.ERROR, 0xBF2200, Level.FATAL, 0xFF8484); + + static DiscordLogMessage ofEvent(LogEvent event) { + String authorName = event.getLoggerName(); + String title = event.getLevel().name(); + int colorDecimal = Objects.requireNonNull(LEVEL_TO_AMBIENT_COLOR.get(event.getLevel())); + String description = event.getMessage().getFormattedMessage(); + String timestampIsoText = + Instant.ofEpochMilli(event.getInstant().getEpochMillisecond()).toString(); + + DiscordLogMessageEmbedAuthor author = new DiscordLogMessageEmbedAuthor(authorName); + DiscordLogMessageEmbed embed = new DiscordLogMessageEmbed(author, title, description, + colorDecimal, timestampIsoText); + return new DiscordLogMessage(List.of(embed)); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbed.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbed.java new file mode 100644 index 0000000000..0ed11dde8d --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbed.java @@ -0,0 +1,10 @@ +package org.togetherjava.tjbot.logging.discord; + +/** + * The Jackson representation of Discords Webhook embeds: API + * Documentation + */ +record DiscordLogMessageEmbed(DiscordLogMessageEmbedAuthor author, String title, String description, + int color, String timestamp) { +} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbedAuthor.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbedAuthor.java new file mode 100644 index 0000000000..38d47e82d5 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbedAuthor.java @@ -0,0 +1,9 @@ +package org.togetherjava.tjbot.logging.discord; + +/** + * The Jackson representation of Discords Webhook embeds author: API + * Documentation + */ +record DiscordLogMessageEmbedAuthor(String name) { +} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java new file mode 100644 index 0000000000..8206669a47 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java @@ -0,0 +1,37 @@ +package org.togetherjava.tjbot.logging.discord; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; + +import java.net.URI; + +// FIXME Javadoc +public final class DiscordLogging { + private DiscordLogging() { + throw new UnsupportedOperationException("Utility class"); + } + + // FIXME Javadoc + public static void appendDiscordLogAppender(String webhook) { + URI webhookUri; + try { + webhookUri = URI.create(webhook); + } catch (IllegalArgumentException e) { + // FIXME Some error reporting + return; + } + + LoggerContext context = (LoggerContext) LogManager.getContext(false); + Configuration configuration = context.getConfiguration(); + + Appender appender = + DiscordLogAppender.newBuilder().setName("Discord").setWebhook(webhookUri).build(); + appender.start(); + configuration.addAppender(appender); + configuration.getRootLogger().addAppender(appender, null, null); + + context.updateLoggers(); + } +} From 641259c7bb56d36af4c177e7b30b29ae5baf2966 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 23 Sep 2022 23:31:26 +0200 Subject: [PATCH 2/8] rate limiting, batch sending, scheduling --- .../logging/discord/DiscordLogAppender.java | 30 +-- .../logging/discord/DiscordLogForwarder.java | 172 ++++++++++++++++++ .../logging/discord/DiscordLogMessage.java | 34 ---- .../discord/DiscordLogMessageEmbed.java | 10 - .../logging/discord/api/DiscordLogBatch.java | 11 ++ .../discord/api/DiscordLogMessageEmbed.java | 59 ++++++ .../DiscordLogMessageEmbedAuthor.java | 2 +- .../logging/discord/api/package-info.java | 12 ++ .../tjbot/logging/discord/package-info.java | 11 ++ 9 files changed, 269 insertions(+), 72 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessage.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbed.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogBatch.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbed.java rename application/src/main/java/org/togetherjava/tjbot/logging/discord/{ => api}/DiscordLogMessageEmbedAuthor.java (82%) create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/api/package-info.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/package-info.java diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogAppender.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogAppender.java index e85a4a74e3..c826e5066e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogAppender.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogAppender.java @@ -1,7 +1,5 @@ package org.togetherjava.tjbot.logging.discord; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.logging.log4j.core.*; import org.apache.logging.log4j.core.appender.AbstractAppender; import org.apache.logging.log4j.core.config.Property; @@ -12,46 +10,24 @@ import java.io.Serializable; import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.util.Objects; @Plugin(name = "Discord", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE) final class DiscordLogAppender extends AbstractAppender { - private static final Property[] NO_PROPERTIES = {}; - private static final HttpClient CLIENT = HttpClient.newHttpClient(); - private static final ObjectMapper JSON = new ObjectMapper(); - private final URI webhook; + private final DiscordLogForwarder logForwarder; private DiscordLogAppender(String name, Filter filter, StringLayout layout, boolean ignoreExceptions, URI webhook) { super(name, filter, layout, ignoreExceptions, NO_PROPERTIES); - this.webhook = webhook; + logForwarder = new DiscordLogForwarder(webhook); } @Override public void append(LogEvent event) { - DiscordLogMessage message = DiscordLogMessage.ofEvent(event); - String messageRaw; - try { - messageRaw = JSON.writeValueAsString(message); - } catch (JsonProcessingException e) { - // FIXME Some error reporting - return; - } - - HttpRequest request = HttpRequest.newBuilder(webhook) - .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(messageRaw)) - .build(); - - // Fire and forget - CLIENT.sendAsync(request, HttpResponse.BodyHandlers.discarding()); - // FIXME 429 Rate limiting, Retry-After + logForwarder.forwardLogEvent(event); } @PluginBuilderFactory diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java new file mode 100644 index 0000000000..b24445e3b5 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java @@ -0,0 +1,172 @@ +package org.togetherjava.tjbot.logging.discord; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.logging.log4j.core.LogEvent; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.logging.discord.api.DiscordLogBatch; +import org.togetherjava.tjbot.logging.discord.api.DiscordLogMessageEmbed; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Instant; +import java.util.List; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; + +// FIXME This class needs some javadoc here and there +final class DiscordLogForwarder { + private static final int MAX_PENDING_LOGS = 10_000; + private static final int MAX_BATCH_SIZE = 10; + private static final int HTTP_STATUS_TOO_MANY_REQUESTS = 429; + private static final int MAX_RETRIES_UNTIL_DISCARD = 3; + + private static final HttpClient CLIENT = HttpClient.newHttpClient(); + private static final ObjectMapper JSON = new ObjectMapper(); + private static final ScheduledExecutorService SERVICE = + Executors.newSingleThreadScheduledExecutor(); + + private final URI webhook; + private final Queue pendingLogs = new PriorityQueue<>(); + private final Object pendingLogsLock = new Object(); + private Instant rateLimitExpiresAt; + private int currentRetries = 0; + + DiscordLogForwarder(URI webhook) { + this.webhook = webhook; + + SERVICE.scheduleWithFixedDelay(this::processPendingLogs, 5, 5, TimeUnit.SECONDS); + } + + void forwardLogEvent(LogEvent event) { + if (pendingLogs.size() >= MAX_PENDING_LOGS) { + // FIXME Discard log, some error reporting + return; + } + + LogMessage log = LogMessage.ofEvent(event); + + synchronized (pendingLogsLock) { + pendingLogs.add(log); + } + } + + private void processPendingLogs() { + try { + if (rateLimitExpiresAt != null) { + if (Instant.now().isBefore(rateLimitExpiresAt)) { + // Still rate limited, try again later + return; + } + + // Rate limit has expired, try again + rateLimitExpiresAt = null; + } + + // Process batch + List logsToProcess = pollLogsToProcessBatch(); + if (logsToProcess.isEmpty()) { + return; + } + + List embeds = + logsToProcess.stream().map(LogMessage::embed).toList(); + DiscordLogBatch logBatch = new DiscordLogBatch(embeds); + + LogSendResult result = sendLogBatch(logBatch); + if (result != LogSendResult.SUCCESS) { + currentRetries++; + if (currentRetries <= MAX_RETRIES_UNTIL_DISCARD) { + synchronized (pendingLogsLock) { + // Try again later + pendingLogs.addAll(logsToProcess); + } + } else { + // Discarding problematic batch + // FIXME some error reporting + } + } else { + currentRetries = 0; + } + } catch (Exception e) { + // FIXME some error reporting + } + } + + private List pollLogsToProcessBatch() { + int batchSize = Math.min(pendingLogs.size(), MAX_BATCH_SIZE); + synchronized (pendingLogsLock) { + return Stream.generate(pendingLogs::remove).limit(batchSize).toList(); + } + } + + private LogSendResult sendLogBatch(DiscordLogBatch logBatch) { + String rawPayload; + try { + rawPayload = JSON.writeValueAsString(logBatch); + } catch (JsonProcessingException e) { + // FIXME Some error reporting + throw new IllegalArgumentException(e); + } + + HttpRequest request = HttpRequest.newBuilder(webhook) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(rawPayload)) + .build(); + + try { + HttpResponse response = + CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); + + if (response.statusCode() == HTTP_STATUS_TOO_MANY_REQUESTS) { + response.headers() + .firstValueAsLong("Retry-After") + .ifPresent(retryAfterSeconds -> rateLimitExpiresAt = + Instant.now().plusSeconds(retryAfterSeconds)); + // FIXME Some logging + return LogSendResult.RATE_LIMIT; + } + if (response.statusCode() < 200 || response.statusCode() >= 300) { + // FIXME Some logging + return LogSendResult.ERROR; + } + + return LogSendResult.SUCCESS; + } catch (IOException e) { + // FIXME Some logging + return LogSendResult.ERROR; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return LogSendResult.ERROR; + } + } + + private record LogMessage(DiscordLogMessageEmbed embed, + Instant timestamp) implements Comparable { + private static LogMessage ofEvent(LogEvent event) { + DiscordLogMessageEmbed embed = DiscordLogMessageEmbed.ofEvent(event); + Instant timestamp = Instant.ofEpochMilli(event.getInstant().getEpochMillisecond()); + + return new LogMessage(embed, timestamp); + } + + @Override + public int compareTo(@NotNull LogMessage o) { + return timestamp.compareTo(o.timestamp); + } + } + + + private enum LogSendResult { + SUCCESS, + RATE_LIMIT, + ERROR + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessage.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessage.java deleted file mode 100644 index fa349d411a..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessage.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.togetherjava.tjbot.logging.discord; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.LogEvent; - -import java.time.Instant; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -/** - * The Jackson representation of Discords Webhook messages: API - * Documentation - */ -record DiscordLogMessage(List embeds) { - private static final Map LEVEL_TO_AMBIENT_COLOR = - Map.of(Level.TRACE, 0x00B362, Level.DEBUG, 0x00A5CE, Level.INFO, 0xAC59FF, Level.WARN, - 0xDFDF00, Level.ERROR, 0xBF2200, Level.FATAL, 0xFF8484); - - static DiscordLogMessage ofEvent(LogEvent event) { - String authorName = event.getLoggerName(); - String title = event.getLevel().name(); - int colorDecimal = Objects.requireNonNull(LEVEL_TO_AMBIENT_COLOR.get(event.getLevel())); - String description = event.getMessage().getFormattedMessage(); - String timestampIsoText = - Instant.ofEpochMilli(event.getInstant().getEpochMillisecond()).toString(); - - DiscordLogMessageEmbedAuthor author = new DiscordLogMessageEmbedAuthor(authorName); - DiscordLogMessageEmbed embed = new DiscordLogMessageEmbed(author, title, description, - colorDecimal, timestampIsoText); - return new DiscordLogMessage(List.of(embed)); - } -} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbed.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbed.java deleted file mode 100644 index 0ed11dde8d..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbed.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.togetherjava.tjbot.logging.discord; - -/** - * The Jackson representation of Discords Webhook embeds: API - * Documentation - */ -record DiscordLogMessageEmbed(DiscordLogMessageEmbedAuthor author, String title, String description, - int color, String timestamp) { -} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogBatch.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogBatch.java new file mode 100644 index 0000000000..d2bafe642b --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogBatch.java @@ -0,0 +1,11 @@ +package org.togetherjava.tjbot.logging.discord.api; + +import java.util.List; + +/** + * The Jackson representation of Discords Webhook messages: API + * Documentation + */ +public record DiscordLogBatch(List embeds) { +} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbed.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbed.java new file mode 100644 index 0000000000..833f4172ed --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbed.java @@ -0,0 +1,59 @@ +package org.togetherjava.tjbot.logging.discord.api; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.LogEvent; + +import java.time.Instant; +import java.util.Map; +import java.util.Objects; + +/** + * The Jackson representation of Discords Webhook embeds: API + * Documentation + */ +public record DiscordLogMessageEmbed(DiscordLogMessageEmbedAuthor author, String title, + String description, int color, String timestamp) { + + /** + * Has to be small enough for fitting all embeds in {@link DiscordLogBatch} into the total + * character length of ~6000. + */ + private static final int MAX_EMBED_DESCRIPTION = 1_000; + private static final Map LEVEL_TO_AMBIENT_COLOR = + Map.of(Level.TRACE, 0x00B362, Level.DEBUG, 0x00A5CE, Level.INFO, 0xAC59FF, Level.WARN, + 0xDFDF00, Level.ERROR, 0xBF2200, Level.FATAL, 0xFF8484); + + /** + * Creates an instance representing the information from the given log event. + * + * @param event the event to extract log info from + * @return an instance representing the given log + */ + public static DiscordLogMessageEmbed ofEvent(LogEvent event) { + String authorName = event.getLoggerName(); + String title = event.getLevel().name(); + int colorDecimal = Objects.requireNonNull(LEVEL_TO_AMBIENT_COLOR.get(event.getLevel())); + String description = + abbreviate(event.getMessage().getFormattedMessage(), MAX_EMBED_DESCRIPTION); + String timestampIsoText = + Instant.ofEpochMilli(event.getInstant().getEpochMillisecond()).toString(); + + DiscordLogMessageEmbedAuthor author = new DiscordLogMessageEmbedAuthor(authorName); + + return new DiscordLogMessageEmbed(author, title, description, colorDecimal, + timestampIsoText); + } + + private static String abbreviate(String text, int maxLength) { + if (text.length() < maxLength) { + return text; + } + + if (maxLength < 3) { + return text.substring(0, maxLength); + } + + return text.substring(0, maxLength - 3) + "..."; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbedAuthor.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbedAuthor.java similarity index 82% rename from application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbedAuthor.java rename to application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbedAuthor.java index 38d47e82d5..a352d1ba4a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogMessageEmbedAuthor.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbedAuthor.java @@ -1,4 +1,4 @@ -package org.togetherjava.tjbot.logging.discord; +package org.togetherjava.tjbot.logging.discord.api; /** * The Jackson representation of Discords Webhook embeds author: API + * Documentation + */ +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +package org.togetherjava.tjbot.logging.discord.api; + +import org.togetherjava.tjbot.annotations.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/package-info.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/package-info.java new file mode 100644 index 0000000000..d81c9230a2 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/package-info.java @@ -0,0 +1,11 @@ +/** + * Contains a Discord log forwarder plugin, accessible by + * {@link org.togetherjava.tjbot.logging.discord.DiscordLogging}. + */ +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +package org.togetherjava.tjbot.logging.discord; + +import org.togetherjava.tjbot.annotations.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; From 9abeda99670c7224dce1f2509a5c232fb60e9d20 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Sat, 24 Sep 2022 23:46:20 +0200 Subject: [PATCH 3/8] Error handling and info/error channel split --- application/config.json.template | 3 +- .../org/togetherjava/tjbot/Application.java | 5 +- .../componentids/ComponentIdStore.java | 5 +- .../tjbot/commands/moderation/BanCommand.java | 6 +- .../commands/moderation/KickCommand.java | 4 +- .../commands/moderation/MuteCommand.java | 4 +- .../commands/moderation/NoteCommand.java | 4 +- .../moderation/QuarantineCommand.java | 4 +- .../RejoinModerationRoleListener.java | 4 +- .../commands/moderation/UnbanCommand.java | 16 ++-- .../commands/moderation/UnmuteCommand.java | 4 +- .../moderation/UnquarantineCommand.java | 4 +- .../commands/moderation/WarnCommand.java | 3 +- .../commands/moderation/scam/ScamBlocker.java | 12 +-- .../temp/RevocableRoleBasedAction.java | 5 +- .../moderation/temp/TemporaryBanAction.java | 5 +- .../temp/TemporaryModerationRoutine.java | 7 +- .../tjbot/commands/tags/TagManageCommand.java | 5 +- .../org/togetherjava/tjbot/config/Config.java | 24 ++++-- .../tjbot/logging/LogMarkers.java | 20 +++++ .../logging/discord/DiscordLogForwarder.java | 46 +++++++---- .../tjbot/logging/discord/DiscordLogging.java | 76 ++++++++++++++++--- 22 files changed, 198 insertions(+), 68 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/LogMarkers.java diff --git a/application/config.json.template b/application/config.json.template index db14a383d0..d7a3563a38 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -84,5 +84,6 @@ "wsf", "wsh" ], - "logChannelWebhook": "" + "logInfoChannelWebhook": "", + "logErrorChannelWebhook": "" } diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index f2af6c959a..2d9490bf64 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -12,6 +12,7 @@ import org.togetherjava.tjbot.commands.system.BotCore; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; +import org.togetherjava.tjbot.logging.LogMarkers; import org.togetherjava.tjbot.logging.discord.DiscordLogging; import java.io.IOException; @@ -58,7 +59,7 @@ public static void main(final String[] args) { Thread.setDefaultUncaughtExceptionHandler(Application::onUncaughtException); Runtime.getRuntime().addShutdownHook(new Thread(Application::onShutdown)); - DiscordLogging.appendDiscordLogAppender(config.getLogChannelWebhook()); + DiscordLogging.startDiscordLogging(config); runBot(config); } @@ -94,7 +95,7 @@ public static void runBot(Config config) { logger.info("Bot is ready"); } catch (InvalidTokenException e) { - logger.error("Failed to login", e); + logger.error(LogMarkers.SENSITIVE, "Failed to login", e); } catch (InterruptedException e) { logger.error("Interrupted while waiting for setup to complete", e); Thread.currentThread().interrupt(); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java index cbbf106f50..7883c280ae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java @@ -12,6 +12,7 @@ import org.togetherjava.tjbot.db.Database; import org.togetherjava.tjbot.db.generated.tables.ComponentIds; import org.togetherjava.tjbot.db.generated.tables.records.ComponentIdsRecord; +import org.togetherjava.tjbot.logging.LogMarkers; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -323,8 +324,8 @@ private void logDebugSizeStatistics() { ComponentIds.COMPONENT_IDS.LIFESPAN.eq(lifespan.name()))))); int recordsCount = lifespanToCount.values().stream().mapToInt(Integer::intValue).sum(); - logger.debug("The component id store consists of {} records ({})", recordsCount, - lifespanToCount); + logger.debug(LogMarkers.SENSITIVE, "The component id store consists of {} records ({})", + recordsCount, lifespanToCount); } @Override diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index a92d387c2e..01596d37a1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.logging.LogMarkers; import javax.annotation.Nullable; import java.time.Instant; @@ -128,7 +129,8 @@ private static Optional> handleNotAlreadyBannedRespo .setEphemeral(true)); } } - logger.warn("Something unexpected went wrong while trying to ban the user '{}'.", + logger.warn(LogMarkers.SENSITIVE, + "Something unexpected went wrong while trying to ban the user '{}'.", target.getAsTag(), alreadyBannedFailure); return Optional.of(event.reply("Failed to ban the user due to an unexpected problem.") .setEphemeral(true)); @@ -149,7 +151,7 @@ private AuditableRestAction banUser(User target, Member author, int deleteHistoryDays, Guild guild) { String durationMessage = temporaryData == null ? "permanently" : "for " + temporaryData.duration(); - logger.info( + logger.info(LogMarkers.SENSITIVE, "'{}' ({}) banned the user '{}' ({}) {} from guild '{}' and deleted their message history of the last {} days, for reason '{}'.", author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(), durationMessage, guild.getName(), deleteHistoryDays, reason); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index b237cecd07..dc88789e44 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.logging.LogMarkers; import javax.annotation.Nullable; import java.util.Objects; @@ -82,7 +83,8 @@ private static RestAction sendDm(ISnowflake target, String reason, Guil private AuditableRestAction kickUser(Member target, Member author, String reason, Guild guild) { - logger.info("'{}' ({}) kicked the user '{}' ({}) from guild '{}' for reason '{}'.", + logger.info(LogMarkers.SENSITIVE, + "'{}' ({}) kicked the user '{}' ({}) from guild '{}' for reason '{}'.", author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(), target.getId(), guild.getName(), reason); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java index e977196cc0..8102a2e646 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java @@ -14,6 +14,7 @@ import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.logging.LogMarkers; import javax.annotation.Nullable; import java.time.Instant; @@ -103,7 +104,8 @@ private AuditableRestAction muteUser(Member target, Member author, @Nullable ModerationUtils.TemporaryData temporaryData, String reason, Guild guild) { String durationMessage = temporaryData == null ? "permanently" : "for " + temporaryData.duration(); - logger.info("'{}' ({}) muted the user '{}' ({}) {} in guild '{}' for reason '{}'.", + logger.info(LogMarkers.SENSITIVE, + "'{}' ({}) muted the user '{}' ({}) {} in guild '{}' for reason '{}'.", author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(), target.getId(), durationMessage, guild.getName(), reason); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java index fc5017d170..803a664302 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.logging.LogMarkers; import javax.annotation.Nullable; import java.util.Objects; @@ -77,7 +78,8 @@ private void sendNote(User target, Member author, String content, ISnowflake gui } private void storeNote(User target, Member author, String content, ISnowflake guild) { - logger.info("'{}' ({}) wrote a note about the user '{}' ({}) with content '{}'.", + logger.info(LogMarkers.SENSITIVE, + "'{}' ({}) wrote a note about the user '{}' ({}) with content '{}'.", author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(), content); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java index a75d73fdd2..9599db922c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java @@ -13,6 +13,7 @@ import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.logging.LogMarkers; import javax.annotation.Nullable; import java.util.Objects; @@ -88,7 +89,8 @@ private static MessageEmbed sendFeedback(boolean hasSentDm, Member target, Membe private AuditableRestAction quarantineUser(Member target, Member author, String reason, Guild guild) { - logger.info("'{}' ({}) quarantined the user '{}' ({}) in guild '{}' for reason '{}'.", + logger.info(LogMarkers.SENSITIVE, + "'{}' ({}) quarantined the user '{}' ({}) in guild '{}' for reason '{}'.", author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(), target.getId(), guild.getName(), reason); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/RejoinModerationRoleListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/RejoinModerationRoleListener.java index 0df83b86d3..cec8f35260 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/RejoinModerationRoleListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/RejoinModerationRoleListener.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.EventReceiver; import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.logging.LogMarkers; import java.util.List; import java.util.Optional; @@ -93,7 +94,8 @@ private boolean shouldApplyModerationRole(ModerationRole moderationRole, private static void applyModerationRole(ModerationRole moderationRole, Member member) { Guild guild = member.getGuild(); - logger.info("Reapplied existing {} to user '{}' ({}) in guild '{}' after rejoining.", + logger.info(LogMarkers.SENSITIVE, + "Reapplied existing {} to user '{}' ({}) in guild '{}' after rejoining.", moderationRole.actionName, member.getUser().getAsTag(), member.getId(), guild.getName()); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index 3aea534970..0bc6d5746b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.logging.LogMarkers; import java.util.Objects; @@ -49,7 +50,8 @@ private void unban(User target, Member author, String reason, Guild guild, ModerationAction.UNBAN, target, null, reason); event.replyEmbeds(message).queue(); - logger.info("'{}' ({}) unbanned the user '{}' ({}) from guild '{}' for reason '{}'.", + logger.info(LogMarkers.SENSITIVE, + "'{}' ({}) unbanned the user '{}' ({}) from guild '{}' for reason '{}'.", author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(), guild.getName(), reason); @@ -63,21 +65,23 @@ private static void handleFailure(Throwable unbanFailure, User target, IReplyCal if (unbanFailure instanceof ErrorResponseException errorResponseException) { if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { event.reply("The specified user does not exist.").setEphemeral(true).queue(); - logger.debug("Unable to unban the user '{}' because they do not exist.", targetTag); + logger.debug(LogMarkers.SENSITIVE, + "Unable to unban the user '{}' because they do not exist.", targetTag); return; } if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_BAN) { event.reply("The specified user is not banned.").setEphemeral(true).queue(); - logger.debug("Unable to unban the user '{}' because they are not banned.", - targetTag); + logger.debug(LogMarkers.SENSITIVE, + "Unable to unban the user '{}' because they are not banned.", targetTag); return; } } event.reply("Sorry, but something went wrong.").setEphemeral(true).queue(); - logger.warn("Something unexpected went wrong while trying to unban the user '{}'.", - targetTag, unbanFailure); + logger.warn(LogMarkers.SENSITIVE, + "Something unexpected went wrong while trying to unban the user '{}'.", targetTag, + unbanFailure); } private boolean handleChecks(IPermissionHolder bot, Member author, CharSequence reason, diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java index 48e61b86d1..dfd77ac67e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java @@ -13,6 +13,7 @@ import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.logging.LogMarkers; import javax.annotation.Nullable; import java.util.Objects; @@ -81,7 +82,8 @@ private static MessageEmbed sendFeedback(boolean hasSentDm, Member target, Membe private AuditableRestAction unmuteUser(Member target, Member author, String reason, Guild guild) { - logger.info("'{}' ({}) unmuted the user '{}' ({}) in guild '{}' for reason '{}'.", + logger.info(LogMarkers.SENSITIVE, + "'{}' ({}) unmuted the user '{}' ({}) in guild '{}' for reason '{}'.", author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(), target.getId(), guild.getName(), reason); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java index 7736e9b87c..6619c7ce12 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java @@ -13,6 +13,7 @@ import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.logging.LogMarkers; import javax.annotation.Nullable; import java.util.Objects; @@ -86,7 +87,8 @@ private static MessageEmbed sendFeedback(boolean hasSentDm, Member target, Membe private AuditableRestAction unquarantineUser(Member target, Member author, String reason, Guild guild) { - logger.info("'{}' ({}) unquarantined the user '{}' ({}) in guild '{}' for reason '{}'.", + logger.info(LogMarkers.SENSITIVE, + "'{}' ({}) unquarantined the user '{}' ({}) in guild '{}' for reason '{}'.", author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(), target.getId(), guild.getName(), reason); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java index e518e5c5da..ef7073dea3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java @@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.logging.LogMarkers; import javax.annotation.Nullable; import java.util.Objects; @@ -69,7 +70,7 @@ private static RestAction dmUser(ISnowflake target, String reason, Guil } private void warnUser(User target, Member author, String reason, Guild guild) { - logger.info("'{}' ({}) warned the user '{}' ({}) for reason '{}'.", + logger.info(LogMarkers.SENSITIVE, "'{}' ({}) warned the user '{}' ({}) for reason '{}'.", author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(), reason); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java index 5cf745fdde..f12e547944 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java @@ -26,6 +26,7 @@ import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.config.ScamBlockerConfig; +import org.togetherjava.tjbot.logging.LogMarkers; import java.awt.Color; import java.util.*; @@ -176,7 +177,8 @@ private void addScamToHistory(MessageReceivedEvent event) { } private void logScamMessage(MessageReceivedEvent event) { - logger.warn("Detected a scam message ('{}') from user '{}' in channel '{}' of guild '{}'.", + logger.warn(LogMarkers.SENSITIVE, + "Detected a scam message ('{}') from user '{}' in channel '{}' of guild '{}'.", event.getMessageId(), event.getAuthor().getId(), event.getChannel().getId(), event.getGuild().getId()); } @@ -281,7 +283,7 @@ public void onButtonClick(ButtonInteractionEvent event, List argsRaw) { MessageUtils.disableButtons(event.getMessage()); event.deferEdit().queue(); if (event.getButton().getStyle() == ButtonStyle.DANGER) { - logger.info( + logger.info(LogMarkers.SENSITIVE, "Identified a false-positive scam (id '{}', hash '{}') in guild '{}' sent by author '{}'", args.messageId, args.contentHash, args.guildId, args.authorId); return; @@ -307,7 +309,7 @@ public void onButtonClick(ButtonInteractionEvent event, List argsRaw) { TextChannel channel = guild.getTextChannelById(scamMessage.channelId()); if (channel == null) { logger.debug( - "Attempted to delete scam messages, bot the channel '{}' does not exist anymore, skipping deleting messages for this channel.", + "Attempted to delete scam messages, but the channel '{}' does not exist anymore, skipping deleting messages for this channel.", scamMessage.channelId()); return; } @@ -318,10 +320,10 @@ public void onButtonClick(ButtonInteractionEvent event, List argsRaw) { Consumer onRetrieveAuthorFailure = new ErrorHandler() .handle(ErrorResponse.UNKNOWN_USER, - failure -> logger.debug( + failure -> logger.debug(LogMarkers.SENSITIVE, "Attempted to handle scam, but user '{}' does not exist anymore.", args.authorId)) - .handle(ErrorResponse.UNKNOWN_MEMBER, failure -> logger.debug( + .handle(ErrorResponse.UNKNOWN_MEMBER, failure -> logger.debug(LogMarkers.SENSITIVE, "Attempted to handle scam, but user '{}' is not a member of the guild anymore.", args.authorId)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java index 1e5e61b7e4..ed60da7694 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -3,6 +3,7 @@ import net.dv8tion.jda.api.exceptions.ErrorResponseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.logging.LogMarkers; /** * Role based moderation actions that can be revoked, for example a {@link TemporaryMuteAction} or a @@ -28,10 +29,10 @@ public FailureIdentification handleRevokeFailure(Throwable failure, long targetI if (failure instanceof ErrorResponseException errorResponseException) { switch (errorResponseException.getErrorResponse()) { - case UNKNOWN_USER -> logger.debug( + case UNKNOWN_USER -> logger.debug(LogMarkers.SENSITIVE, "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", actionName, targetId); - case UNKNOWN_MEMBER -> logger.debug( + case UNKNOWN_MEMBER -> logger.debug(LogMarkers.SENSITIVE, "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", actionName, targetId); case UNKNOWN_ROLE -> logger.warn( diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java index 1e6cdb779a..bedb8374dc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java @@ -8,6 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; +import org.togetherjava.tjbot.logging.LogMarkers; /** * Action to revoke temporary bans, as applied by @@ -36,14 +37,14 @@ public RestAction revokeAction(Guild guild, User target, String reason) { public FailureIdentification handleRevokeFailure(Throwable failure, long targetId) { if (failure instanceof ErrorResponseException errorResponseException) { if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - logger.debug( + logger.debug(LogMarkers.SENSITIVE, "Attempted to revoke a temporary ban but user '{}' does not exist anymore.", targetId); return FailureIdentification.KNOWN; } if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_BAN) { - logger.debug( + logger.debug(LogMarkers.SENSITIVE, "Attempted to revoke a temporary ban but the user '{}' is not banned anymore.", targetId); return FailureIdentification.KNOWN; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java index 0cf42e45cd..d989bc6895 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java @@ -11,6 +11,7 @@ import org.togetherjava.tjbot.commands.moderation.ModerationAction; import org.togetherjava.tjbot.commands.moderation.ModerationActionsStore; import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.logging.LogMarkers; import java.util.Map; import java.util.Objects; @@ -123,8 +124,8 @@ private void revokeAction(RevocationGroupIdentifier groupIdentifier) { private RestAction executeRevocation(Guild guild, User target, ModerationAction actionType) { - logger.info("Revoked temporary action {} against user '{}' ({}).", actionType, - target.getAsTag(), target.getId()); + logger.info(LogMarkers.SENSITIVE, "Revoked temporary action {} against user '{}' ({}).", + actionType, target.getAsTag(), target.getId()); RevocableModerationAction action = getRevocableActionByType(actionType); String reason = "Automatic revocation of temporary action."; @@ -140,7 +141,7 @@ private void handleFailure(Throwable failure, RevocationGroupIdentifier groupIde return; } - logger.warn( + logger.warn(LogMarkers.SENSITIVE, "Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.", groupIdentifier.targetId, failure); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index bcb3939834..d72a4e1b14 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -296,9 +296,8 @@ private Optional getTagContent(Subcommand subcommand, String id) { } catch (NoSuchElementException e) { // NOTE Rare race condition, for example if another thread deleted the tag in the // meantime - logger.warn(String.format( - "tried to retrieve content of tag '%s', but the content doesn't exist.", - id)); + logger.warn("Tried to retrieve content of tag '{}', but the content doesn't exist.", + id); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 9aa57ba3d2..ec2b79e9b7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -32,7 +32,8 @@ public final class Config { private final HelpSystemConfig helpSystem; private final List blacklistedFileExtension; private final String mediaOnlyChannelPattern; - private final String logChannelWebhook; + private final String logInfoChannelWebhook; + private final String logErrorChannelWebhook; @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) @@ -60,7 +61,8 @@ private Config(@JsonProperty(value = "token", required = true) String token, required = true) String mediaOnlyChannelPattern, @JsonProperty(value = "blacklistedFileExtension", required = true) List blacklistedFileExtension, - @JsonProperty(value = "logChannelWebhook", required = true) String logChannelWebhook) { + @JsonProperty(value = "logInfoChannelWebhook", required = true) String logInfoChannelWebhook, + @JsonProperty(value = "logErrorChannelWebhook", required = true) String logErrorChannelWebhook) { this.token = Objects.requireNonNull(token); this.gistApiKey = Objects.requireNonNull(gistApiKey); this.databasePath = Objects.requireNonNull(databasePath); @@ -78,7 +80,8 @@ private Config(@JsonProperty(value = "token", required = true) String token, this.helpSystem = Objects.requireNonNull(helpSystem); this.mediaOnlyChannelPattern = Objects.requireNonNull(mediaOnlyChannelPattern); this.blacklistedFileExtension = Objects.requireNonNull(blacklistedFileExtension); - this.logChannelWebhook = Objects.requireNonNull(logChannelWebhook); + this.logInfoChannelWebhook = Objects.requireNonNull(logInfoChannelWebhook); + this.logErrorChannelWebhook = Objects.requireNonNull(logErrorChannelWebhook); } /** @@ -254,11 +257,20 @@ public List getBlacklistedFileExtensions() { } /** - * The Discord channel webhook for posting log messages. + * The Discord channel webhook for posting log messages with levels INFO, DEBUG and TRACE. * * @return the webhook URL */ - public String getLogChannelWebhook() { - return logChannelWebhook; + public String getLogInfoChannelWebhook() { + return logInfoChannelWebhook; + } + + /** + * The Discord channel webhook for posting log messages with levels FATAL, ERROR and WARNING. + * + * @return the webhook URL + */ + public String getLogErrorChannelWebhook() { + return logErrorChannelWebhook; } } diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/LogMarkers.java b/application/src/main/java/org/togetherjava/tjbot/logging/LogMarkers.java new file mode 100644 index 0000000000..82c7b59181 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/logging/LogMarkers.java @@ -0,0 +1,20 @@ +package org.togetherjava.tjbot.logging; + +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; + +public final class LogMarkers { + /** + * Log should not be forwarded to Discord. + */ + public static Marker NO_DISCORD = MarkerFactory.getMarker("NO_DISCORD"); + /** + * The log contains sensitive information that only moderators and people with similar authority + * should be allowed to view. + */ + public static Marker SENSITIVE = MarkerFactory.getMarker("SENSITIVE"); + + private LogMarkers() { + throw new UnsupportedOperationException("Utility class"); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java index b24445e3b5..a1c1d7e949 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java @@ -4,6 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.logging.log4j.core.LogEvent; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.logging.LogMarkers; import org.togetherjava.tjbot.logging.discord.api.DiscordLogBatch; import org.togetherjava.tjbot.logging.discord.api.DiscordLogMessageEmbed; @@ -23,10 +26,14 @@ // FIXME This class needs some javadoc here and there final class DiscordLogForwarder { + private static final Logger logger = LoggerFactory.getLogger(DiscordLogForwarder.class); + private static final int MAX_PENDING_LOGS = 10_000; private static final int MAX_BATCH_SIZE = 10; - private static final int HTTP_STATUS_TOO_MANY_REQUESTS = 429; private static final int MAX_RETRIES_UNTIL_DISCARD = 3; + private static final int HTTP_STATUS_TOO_MANY_REQUESTS = 429; + private static final int HTTP_STATUS_OK_START = 200; + private static final int HTTP_STATUS_OK_END = 300; private static final HttpClient CLIENT = HttpClient.newHttpClient(); private static final ObjectMapper JSON = new ObjectMapper(); @@ -47,7 +54,10 @@ final class DiscordLogForwarder { void forwardLogEvent(LogEvent event) { if (pendingLogs.size() >= MAX_PENDING_LOGS) { - // FIXME Discard log, some error reporting + logger.warn(LogMarkers.NO_DISCORD, + """ + Exceeded the max amount of logs that can be buffered. \ + Logs are forwarded to Discord slower than they pile up. Discarding the latest log..."""); return; } @@ -81,7 +91,9 @@ private void processPendingLogs() { DiscordLogBatch logBatch = new DiscordLogBatch(embeds); LogSendResult result = sendLogBatch(logBatch); - if (result != LogSendResult.SUCCESS) { + if (result == LogSendResult.SUCCESS) { + currentRetries = 0; + } else { currentRetries++; if (currentRetries <= MAX_RETRIES_UNTIL_DISCARD) { synchronized (pendingLogsLock) { @@ -89,14 +101,14 @@ private void processPendingLogs() { pendingLogs.addAll(logsToProcess); } } else { - // Discarding problematic batch - // FIXME some error reporting + logger.warn(LogMarkers.NO_DISCORD, """ + A log batch keeps failing forwarding to Discord. \ + Discarding the problematic batch."""); } - } else { - currentRetries = 0; } } catch (Exception e) { - // FIXME some error reporting + logger.warn(LogMarkers.NO_DISCORD, + "Unknown error when forwarding pending logs to Discord.", e); } } @@ -112,8 +124,7 @@ private LogSendResult sendLogBatch(DiscordLogBatch logBatch) { try { rawPayload = JSON.writeValueAsString(logBatch); } catch (JsonProcessingException e) { - // FIXME Some error reporting - throw new IllegalArgumentException(e); + throw new IllegalArgumentException("Unable to write JSON for log batch.", e); } HttpRequest request = HttpRequest.newBuilder(webhook) @@ -130,17 +141,24 @@ private LogSendResult sendLogBatch(DiscordLogBatch logBatch) { .firstValueAsLong("Retry-After") .ifPresent(retryAfterSeconds -> rateLimitExpiresAt = Instant.now().plusSeconds(retryAfterSeconds)); - // FIXME Some logging + logger.debug(LogMarkers.NO_DISCORD, + "Hit rate limits when trying to forward log batch to Discord."); return LogSendResult.RATE_LIMIT; } - if (response.statusCode() < 200 || response.statusCode() >= 300) { - // FIXME Some logging + if (response.statusCode() < HTTP_STATUS_OK_START + || response.statusCode() >= HTTP_STATUS_OK_END) { + if (logger.isWarnEnabled()) { + logger.warn(LogMarkers.NO_DISCORD, + "Discord webhook API responded with {} when forwarding log batch. Body: {}", + response.statusCode(), response.body()); + } return LogSendResult.ERROR; } return LogSendResult.SUCCESS; } catch (IOException e) { - // FIXME Some logging + logger.warn(LogMarkers.NO_DISCORD, "Unknown error when sending log batch to Discord.", + e); return LogSendResult.ERROR; } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java index 8206669a47..75c2a5848a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java @@ -1,37 +1,89 @@ package org.togetherjava.tjbot.logging.discord; +import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.filter.CompositeFilter; +import org.apache.logging.log4j.core.filter.LevelRangeFilter; +import org.apache.logging.log4j.core.filter.MarkerFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.logging.LogMarkers; import java.net.URI; +import java.util.Optional; // FIXME Javadoc public final class DiscordLogging { + private static final Logger logger = LoggerFactory.getLogger(DiscordLogging.class); + private DiscordLogging() { throw new UnsupportedOperationException("Utility class"); } // FIXME Javadoc - public static void appendDiscordLogAppender(String webhook) { - URI webhookUri; + public static void startDiscordLogging(Config botConfig) { + LoggerContext context = (LoggerContext) LogManager.getContext(false); + Configuration logConfig = context.getConfiguration(); + + addAppenders(logConfig, botConfig); + + context.updateLoggers(); + } + + private static void addAppenders(Configuration logConfig, Config botConfig) { + parseWebhookUri(botConfig.getLogInfoChannelWebhook()) + .ifPresent(webhookUri -> addDiscordLogAppender("DiscordInfo", createInfoRangeFilter(), + webhookUri, logConfig)); + + parseWebhookUri(botConfig.getLogErrorChannelWebhook()) + .ifPresent(webhookUri -> addDiscordLogAppender("DiscordError", createErrorRangeFilter(), + webhookUri, logConfig)); + } + + private static Optional parseWebhookUri(String webhookUri) { try { - webhookUri = URI.create(webhook); + return Optional.of(URI.create(webhookUri)); } catch (IllegalArgumentException e) { - // FIXME Some error reporting - return; + logger.warn(LogMarkers.NO_DISCORD, + "The webhook URL ({}) in the config is invalid, logs will not be forwarded to Discord.", + webhookUri, e); + return Optional.empty(); } + } - LoggerContext context = (LoggerContext) LogManager.getContext(false); - Configuration configuration = context.getConfiguration(); + private static void addDiscordLogAppender(String name, Filter filter, URI webhookUri, + Configuration logConfig) { + Filter[] filters = {filter, createDenyMarkerFilter(LogMarkers.NO_DISCORD.getName()), + createDenyMarkerFilter(LogMarkers.SENSITIVE.getName())}; + + Appender appender = DiscordLogAppender.newBuilder() + .setName(name) + .setWebhook(webhookUri) + .setFilter(CompositeFilter.createFilters(filters)) + .build(); - Appender appender = - DiscordLogAppender.newBuilder().setName("Discord").setWebhook(webhookUri).build(); appender.start(); - configuration.addAppender(appender); - configuration.getRootLogger().addAppender(appender, null, null); - context.updateLoggers(); + logConfig.addAppender(appender); + logConfig.getRootLogger().addAppender(appender, null, null); + } + + private static Filter createDenyMarkerFilter(String markerName) { + return MarkerFilter.createFilter(markerName, Filter.Result.DENY, Filter.Result.NEUTRAL); + } + + private static Filter createInfoRangeFilter() { + return LevelRangeFilter.createFilter(Level.INFO, Level.TRACE, Filter.Result.NEUTRAL, + Filter.Result.DENY); + } + + private static Filter createErrorRangeFilter() { + return LevelRangeFilter.createFilter(Level.FATAL, Level.WARN, Filter.Result.NEUTRAL, + Filter.Result.DENY); } } From 02371fed37e642304b90f22557e015c560d89ee5 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Mon, 26 Sep 2022 10:19:44 +0200 Subject: [PATCH 4/8] javadoc --- .../tjbot/logging/LogMarkers.java | 7 +++- .../logging/discord/DiscordLogForwarder.java | 42 ++++++++++++++++++- .../tjbot/logging/discord/DiscordLogging.java | 14 ++++++- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/LogMarkers.java b/application/src/main/java/org/togetherjava/tjbot/logging/LogMarkers.java index 82c7b59181..0be74a74ca 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/LogMarkers.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/LogMarkers.java @@ -3,16 +3,19 @@ import org.slf4j.Marker; import org.slf4j.MarkerFactory; +/** + * Markers to be used for tagging logs. Loggers can then be filter based on them. + */ public final class LogMarkers { /** * Log should not be forwarded to Discord. */ - public static Marker NO_DISCORD = MarkerFactory.getMarker("NO_DISCORD"); + public static final Marker NO_DISCORD = MarkerFactory.getMarker("NO_DISCORD"); /** * The log contains sensitive information that only moderators and people with similar authority * should be allowed to view. */ - public static Marker SENSITIVE = MarkerFactory.getMarker("SENSITIVE"); + public static final Marker SENSITIVE = MarkerFactory.getMarker("SENSITIVE"); private LogMarkers() { throw new UnsupportedOperationException("Utility class"); diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java index a1c1d7e949..a4cf027b34 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java @@ -10,6 +10,7 @@ import org.togetherjava.tjbot.logging.discord.api.DiscordLogBatch; import org.togetherjava.tjbot.logging.discord.api.DiscordLogMessageEmbed; +import javax.annotation.Nullable; import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; @@ -24,7 +25,18 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Stream; -// FIXME This class needs some javadoc here and there +/** + * Forwards log events to a Discord channel via a webhook. See {@link #forwardLogEvent(LogEvent)}. + *

+ * Logs are forwarded in correct order, based on their timestamp. They are not forwarded + * immediately, but at a fixed schedule in batches of {@value MAX_BATCH_SIZE} logs. + *

+ * Failed logs are repeated {@value MAX_RETRIES_UNTIL_DISCARD} times until eventually discarded. + *

+ * Although unlikely to hit, the class maximally buffers {@value MAX_PENDING_LOGS} logs until + * discarding further logs. Under normal circumstances, the class can easily handle high loads of + * logs. + */ final class DiscordLogForwarder { private static final Logger logger = LoggerFactory.getLogger(DiscordLogForwarder.class); @@ -40,11 +52,29 @@ final class DiscordLogForwarder { private static final ScheduledExecutorService SERVICE = Executors.newSingleThreadScheduledExecutor(); + /** + * The Discord webhook to send logs to. + */ private final URI webhook; + /** + * Internal buffer of logs that still have to be forwarded to Discord. Actions are synchronized + * using {@link #pendingLogsLock} to ensure thread safety. + */ private final Queue pendingLogs = new PriorityQueue<>(); private final Object pendingLogsLock = new Object(); + + /** + * If present, a rate limit has been hit and further requests must be made only after this + * moment. + */ + @Nullable private Instant rateLimitExpiresAt; - private int currentRetries = 0; + /** + * The amount of subsequent failed requests. Requests are tried + * {@value MAX_RETRIES_UNTIL_DISCARD} times until discarded. Resets to 0 once a request was + * successful. + */ + private int currentRetries; DiscordLogForwarder(URI webhook) { this.webhook = webhook; @@ -52,6 +82,14 @@ final class DiscordLogForwarder { SERVICE.scheduleWithFixedDelay(this::processPendingLogs, 5, 5, TimeUnit.SECONDS); } + /** + * Forwards the given log message to Discord. + *

+ * Logs are not immediately forwarded, but on a schedule. If the maximal buffer size of + * {@value MAX_PENDING_LOGS} is exceeded, logs are discarded. + * + * @param event the log to forward + */ void forwardLogEvent(LogEvent event) { if (pendingLogs.size() >= MAX_PENDING_LOGS) { logger.warn(LogMarkers.NO_DISCORD, diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java index 75c2a5848a..3757f4dfb0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java @@ -17,7 +17,9 @@ import java.net.URI; import java.util.Optional; -// FIXME Javadoc +/** + * Provides solutions for forwarding logs to Discord. See {@link #startDiscordLogging(Config)}. + */ public final class DiscordLogging { private static final Logger logger = LoggerFactory.getLogger(DiscordLogging.class); @@ -25,7 +27,13 @@ private DiscordLogging() { throw new UnsupportedOperationException("Utility class"); } - // FIXME Javadoc + /** + * Sets up and starts the log forwarding to Discord. + *

+ * Disables the feature if the config is set up incorrectly. + * + * @param botConfig to get the logging details from, such as the Discord webhook urls + */ public static void startDiscordLogging(Config botConfig) { LoggerContext context = (LoggerContext) LogManager.getContext(false); Configuration logConfig = context.getConfiguration(); @@ -58,6 +66,8 @@ private static Optional parseWebhookUri(String webhookUri) { private static void addDiscordLogAppender(String name, Filter filter, URI webhookUri, Configuration logConfig) { + // NOTE The whole setup is done programmatically in order to allow the webhooks + // to be read from the config file Filter[] filters = {filter, createDenyMarkerFilter(LogMarkers.NO_DISCORD.getName()), createDenyMarkerFilter(LogMarkers.SENSITIVE.getName())}; From 1325758b0461ce51e7744fceb85810a5ef1c32d0 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Mon, 26 Sep 2022 10:38:43 +0200 Subject: [PATCH 5/8] Sonar logging issue --- .../togetherjava/tjbot/logging/discord/DiscordLogging.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java index 3757f4dfb0..da823cc501 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java @@ -64,6 +64,11 @@ private static Optional parseWebhookUri(String webhookUri) { } } + // Security warning about configuring logs. It is safe in this case, the only user input are the + // webhook URIs, which cannot inject anything malicious. + // The only risk is changing the target to an attackers' server, but therefore they need access + // to the config. + @SuppressWarnings("squid:S4792") private static void addDiscordLogAppender(String name, Filter filter, URI webhookUri, Configuration logConfig) { // NOTE The whole setup is done programmatically in order to allow the webhooks From f3faf719c3f7885595e7b1be1c23c5f7ef6baff3 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 27 Sep 2022 09:16:39 +0200 Subject: [PATCH 6/8] Readability, CR (Mom0auth) --- .../logging/discord/DiscordLogForwarder.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java index a4cf027b34..c60292b2e7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java @@ -108,14 +108,9 @@ void forwardLogEvent(LogEvent event) { private void processPendingLogs() { try { - if (rateLimitExpiresAt != null) { - if (Instant.now().isBefore(rateLimitExpiresAt)) { - // Still rate limited, try again later - return; - } - - // Rate limit has expired, try again - rateLimitExpiresAt = null; + if (handleIsRateLimitActive()) { + // Try again later + return; } // Process batch @@ -150,6 +145,19 @@ private void processPendingLogs() { } } + private boolean handleIsRateLimitActive() { + if (rateLimitExpiresAt == null) { + return false; + } + + if (Instant.now().isAfter(rateLimitExpiresAt)) { + // Rate limit has expired, reset + rateLimitExpiresAt = null; + } + + return true; + } + private List pollLogsToProcessBatch() { int batchSize = Math.min(pendingLogs.size(), MAX_BATCH_SIZE); synchronized (pendingLogsLock) { From d395ad0d15e0cb37f0c276053c330de85e57f0f2 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 30 Sep 2022 13:34:23 +0200 Subject: [PATCH 7/8] SImplified log forwarding by using webhook lib --- application/build.gradle | 2 + .../logging/discord/DiscordLogForwarder.java | 182 +++++------------- .../logging/discord/api/DiscordLogBatch.java | 11 -- .../discord/api/DiscordLogMessageEmbed.java | 59 ------ .../api/DiscordLogMessageEmbedAuthor.java | 9 - .../logging/discord/api/package-info.java | 12 -- 6 files changed, 52 insertions(+), 223 deletions(-) delete mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogBatch.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbed.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbedAuthor.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/logging/discord/api/package-info.java diff --git a/application/build.gradle b/application/build.gradle index 14cd60d0b5..75ede0d319 100644 --- a/application/build.gradle +++ b/application/build.gradle @@ -50,6 +50,8 @@ dependencies { implementation 'org.apache.logging.log4j:log4j-core:2.19.0' runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.18.0' + implementation 'club.minnced:discord-webhooks:0.8.2' + implementation 'org.jooq:jooq:3.17.2' implementation 'io.mikael:urlbuilder:2.0.9' diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java index c60292b2e7..92b087d073 100644 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java +++ b/application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogForwarder.java @@ -1,25 +1,19 @@ package org.togetherjava.tjbot.logging.discord; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import club.minnced.discord.webhook.WebhookClient; +import club.minnced.discord.webhook.send.WebhookEmbed; +import club.minnced.discord.webhook.send.WebhookEmbedBuilder; +import club.minnced.discord.webhook.send.WebhookMessage; +import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.LogEvent; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.logging.LogMarkers; -import org.togetherjava.tjbot.logging.discord.api.DiscordLogBatch; -import org.togetherjava.tjbot.logging.discord.api.DiscordLogMessageEmbed; -import javax.annotation.Nullable; -import java.io.IOException; import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.time.Instant; -import java.util.List; -import java.util.PriorityQueue; -import java.util.Queue; +import java.util.*; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -31,31 +25,29 @@ * Logs are forwarded in correct order, based on their timestamp. They are not forwarded * immediately, but at a fixed schedule in batches of {@value MAX_BATCH_SIZE} logs. *

- * Failed logs are repeated {@value MAX_RETRIES_UNTIL_DISCARD} times until eventually discarded. - *

* Although unlikely to hit, the class maximally buffers {@value MAX_PENDING_LOGS} logs until * discarding further logs. Under normal circumstances, the class can easily handle high loads of * logs. + *

+ * The class is thread-safe. */ final class DiscordLogForwarder { private static final Logger logger = LoggerFactory.getLogger(DiscordLogForwarder.class); private static final int MAX_PENDING_LOGS = 10_000; - private static final int MAX_BATCH_SIZE = 10; - private static final int MAX_RETRIES_UNTIL_DISCARD = 3; - private static final int HTTP_STATUS_TOO_MANY_REQUESTS = 429; - private static final int HTTP_STATUS_OK_START = 200; - private static final int HTTP_STATUS_OK_END = 300; - - private static final HttpClient CLIENT = HttpClient.newHttpClient(); - private static final ObjectMapper JSON = new ObjectMapper(); + private static final int MAX_BATCH_SIZE = WebhookMessage.MAX_EMBEDS; private static final ScheduledExecutorService SERVICE = Executors.newSingleThreadScheduledExecutor(); - /** - * The Discord webhook to send logs to. + * Has to be small enough for fitting all {@value MAX_BATCH_SIZE} embeds contained in a batch + * into the total character length of ~6000. */ - private final URI webhook; + private static final int MAX_EMBED_DESCRIPTION = 1_000; + private static final Map LEVEL_TO_AMBIENT_COLOR = + Map.of(Level.TRACE, 0x00B362, Level.DEBUG, 0x00A5CE, Level.INFO, 0xAC59FF, Level.WARN, + 0xDFDF00, Level.ERROR, 0xBF2200, Level.FATAL, 0xFF8484); + + private final WebhookClient webhookClient; /** * Internal buffer of logs that still have to be forwarded to Discord. Actions are synchronized * using {@link #pendingLogsLock} to ensure thread safety. @@ -63,30 +55,20 @@ final class DiscordLogForwarder { private final Queue pendingLogs = new PriorityQueue<>(); private final Object pendingLogsLock = new Object(); - /** - * If present, a rate limit has been hit and further requests must be made only after this - * moment. - */ - @Nullable - private Instant rateLimitExpiresAt; - /** - * The amount of subsequent failed requests. Requests are tried - * {@value MAX_RETRIES_UNTIL_DISCARD} times until discarded. Resets to 0 once a request was - * successful. - */ - private int currentRetries; - DiscordLogForwarder(URI webhook) { - this.webhook = webhook; + webhookClient = WebhookClient.withUrl(webhook.toString()); SERVICE.scheduleWithFixedDelay(this::processPendingLogs, 5, 5, TimeUnit.SECONDS); } + /** * Forwards the given log message to Discord. *

* Logs are not immediately forwarded, but on a schedule. If the maximal buffer size of * {@value MAX_PENDING_LOGS} is exceeded, logs are discarded. + *

+ * This method is thread-safe. * * @param event the log to forward */ @@ -108,56 +90,21 @@ void forwardLogEvent(LogEvent event) { private void processPendingLogs() { try { - if (handleIsRateLimitActive()) { - // Try again later - return; - } - // Process batch List logsToProcess = pollLogsToProcessBatch(); if (logsToProcess.isEmpty()) { return; } - List embeds = - logsToProcess.stream().map(LogMessage::embed).toList(); - DiscordLogBatch logBatch = new DiscordLogBatch(embeds); - - LogSendResult result = sendLogBatch(logBatch); - if (result == LogSendResult.SUCCESS) { - currentRetries = 0; - } else { - currentRetries++; - if (currentRetries <= MAX_RETRIES_UNTIL_DISCARD) { - synchronized (pendingLogsLock) { - // Try again later - pendingLogs.addAll(logsToProcess); - } - } else { - logger.warn(LogMarkers.NO_DISCORD, """ - A log batch keeps failing forwarding to Discord. \ - Discarding the problematic batch."""); - } - } + List logBatch = logsToProcess.stream().map(LogMessage::embed).toList(); + + webhookClient.send(logBatch); } catch (Exception e) { logger.warn(LogMarkers.NO_DISCORD, "Unknown error when forwarding pending logs to Discord.", e); } } - private boolean handleIsRateLimitActive() { - if (rateLimitExpiresAt == null) { - return false; - } - - if (Instant.now().isAfter(rateLimitExpiresAt)) { - // Rate limit has expired, reset - rateLimitExpiresAt = null; - } - - return true; - } - private List pollLogsToProcessBatch() { int batchSize = Math.min(pendingLogs.size(), MAX_BATCH_SIZE); synchronized (pendingLogsLock) { @@ -165,60 +112,38 @@ private List pollLogsToProcessBatch() { } } - private LogSendResult sendLogBatch(DiscordLogBatch logBatch) { - String rawPayload; - try { - rawPayload = JSON.writeValueAsString(logBatch); - } catch (JsonProcessingException e) { - throw new IllegalArgumentException("Unable to write JSON for log batch.", e); - } + private record LogMessage(WebhookEmbed embed, + Instant timestamp) implements Comparable { - HttpRequest request = HttpRequest.newBuilder(webhook) - .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(rawPayload)) - .build(); + private static LogMessage ofEvent(LogEvent event) { + String authorName = event.getLoggerName(); + String title = event.getLevel().name(); + int colorDecimal = Objects.requireNonNull(LEVEL_TO_AMBIENT_COLOR.get(event.getLevel())); + String description = + abbreviate(event.getMessage().getFormattedMessage(), MAX_EMBED_DESCRIPTION); + Instant timestamp = Instant.ofEpochMilli(event.getInstant().getEpochMillisecond()); - try { - HttpResponse response = - CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); - - if (response.statusCode() == HTTP_STATUS_TOO_MANY_REQUESTS) { - response.headers() - .firstValueAsLong("Retry-After") - .ifPresent(retryAfterSeconds -> rateLimitExpiresAt = - Instant.now().plusSeconds(retryAfterSeconds)); - logger.debug(LogMarkers.NO_DISCORD, - "Hit rate limits when trying to forward log batch to Discord."); - return LogSendResult.RATE_LIMIT; - } - if (response.statusCode() < HTTP_STATUS_OK_START - || response.statusCode() >= HTTP_STATUS_OK_END) { - if (logger.isWarnEnabled()) { - logger.warn(LogMarkers.NO_DISCORD, - "Discord webhook API responded with {} when forwarding log batch. Body: {}", - response.statusCode(), response.body()); - } - return LogSendResult.ERROR; - } + WebhookEmbed embed = new WebhookEmbedBuilder() + .setAuthor(new WebhookEmbed.EmbedAuthor(authorName, null, null)) + .setTitle(new WebhookEmbed.EmbedTitle(title, null)) + .setDescription(description) + .setColor(colorDecimal) + .setTimestamp(timestamp) + .build(); - return LogSendResult.SUCCESS; - } catch (IOException e) { - logger.warn(LogMarkers.NO_DISCORD, "Unknown error when sending log batch to Discord.", - e); - return LogSendResult.ERROR; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return LogSendResult.ERROR; + return new LogMessage(embed, timestamp); } - } - private record LogMessage(DiscordLogMessageEmbed embed, - Instant timestamp) implements Comparable { - private static LogMessage ofEvent(LogEvent event) { - DiscordLogMessageEmbed embed = DiscordLogMessageEmbed.ofEvent(event); - Instant timestamp = Instant.ofEpochMilli(event.getInstant().getEpochMillisecond()); + private static String abbreviate(String text, int maxLength) { + if (text.length() < maxLength) { + return text; + } - return new LogMessage(embed, timestamp); + if (maxLength < 3) { + return text.substring(0, maxLength); + } + + return text.substring(0, maxLength - 3) + "..."; } @Override @@ -226,11 +151,4 @@ public int compareTo(@NotNull LogMessage o) { return timestamp.compareTo(o.timestamp); } } - - - private enum LogSendResult { - SUCCESS, - RATE_LIMIT, - ERROR - } } diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogBatch.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogBatch.java deleted file mode 100644 index d2bafe642b..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogBatch.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.togetherjava.tjbot.logging.discord.api; - -import java.util.List; - -/** - * The Jackson representation of Discords Webhook messages: API - * Documentation - */ -public record DiscordLogBatch(List embeds) { -} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbed.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbed.java deleted file mode 100644 index 833f4172ed..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbed.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.togetherjava.tjbot.logging.discord.api; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.LogEvent; - -import java.time.Instant; -import java.util.Map; -import java.util.Objects; - -/** - * The Jackson representation of Discords Webhook embeds: API - * Documentation - */ -public record DiscordLogMessageEmbed(DiscordLogMessageEmbedAuthor author, String title, - String description, int color, String timestamp) { - - /** - * Has to be small enough for fitting all embeds in {@link DiscordLogBatch} into the total - * character length of ~6000. - */ - private static final int MAX_EMBED_DESCRIPTION = 1_000; - private static final Map LEVEL_TO_AMBIENT_COLOR = - Map.of(Level.TRACE, 0x00B362, Level.DEBUG, 0x00A5CE, Level.INFO, 0xAC59FF, Level.WARN, - 0xDFDF00, Level.ERROR, 0xBF2200, Level.FATAL, 0xFF8484); - - /** - * Creates an instance representing the information from the given log event. - * - * @param event the event to extract log info from - * @return an instance representing the given log - */ - public static DiscordLogMessageEmbed ofEvent(LogEvent event) { - String authorName = event.getLoggerName(); - String title = event.getLevel().name(); - int colorDecimal = Objects.requireNonNull(LEVEL_TO_AMBIENT_COLOR.get(event.getLevel())); - String description = - abbreviate(event.getMessage().getFormattedMessage(), MAX_EMBED_DESCRIPTION); - String timestampIsoText = - Instant.ofEpochMilli(event.getInstant().getEpochMillisecond()).toString(); - - DiscordLogMessageEmbedAuthor author = new DiscordLogMessageEmbedAuthor(authorName); - - return new DiscordLogMessageEmbed(author, title, description, colorDecimal, - timestampIsoText); - } - - private static String abbreviate(String text, int maxLength) { - if (text.length() < maxLength) { - return text; - } - - if (maxLength < 3) { - return text.substring(0, maxLength); - } - - return text.substring(0, maxLength - 3) + "..."; - } -} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbedAuthor.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbedAuthor.java deleted file mode 100644 index a352d1ba4a..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/DiscordLogMessageEmbedAuthor.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.togetherjava.tjbot.logging.discord.api; - -/** - * The Jackson representation of Discords Webhook embeds author: API - * Documentation - */ -record DiscordLogMessageEmbedAuthor(String name) { -} diff --git a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/package-info.java b/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/package-info.java deleted file mode 100644 index 25c2a354ea..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/logging/discord/api/package-info.java +++ /dev/null @@ -1,12 +0,0 @@ -/** - * The Jackson representation of Discords Webhooks: API - * Documentation - */ -@MethodsReturnNonnullByDefault -@ParametersAreNonnullByDefault -package org.togetherjava.tjbot.logging.discord.api; - -import org.togetherjava.tjbot.annotations.MethodsReturnNonnullByDefault; - -import javax.annotation.ParametersAreNonnullByDefault; From b8bbbfe61af2054aba5b9363a0fa558956107ab2 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 4 Oct 2022 10:58:33 +0200 Subject: [PATCH 8/8] Spotless --- .../src/main/java/org/togetherjava/tjbot/config/Config.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index ec2b79e9b7..d9b9d8a88f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -61,8 +61,10 @@ private Config(@JsonProperty(value = "token", required = true) String token, required = true) String mediaOnlyChannelPattern, @JsonProperty(value = "blacklistedFileExtension", required = true) List blacklistedFileExtension, - @JsonProperty(value = "logInfoChannelWebhook", required = true) String logInfoChannelWebhook, - @JsonProperty(value = "logErrorChannelWebhook", required = true) String logErrorChannelWebhook) { + @JsonProperty(value = "logInfoChannelWebhook", + required = true) String logInfoChannelWebhook, + @JsonProperty(value = "logErrorChannelWebhook", + required = true) String logErrorChannelWebhook) { this.token = Objects.requireNonNull(token); this.gistApiKey = Objects.requireNonNull(gistApiKey); this.databasePath = Objects.requireNonNull(databasePath);