From b040d039f150668c578e22b89cad80590c0e225f Mon Sep 17 00:00:00 2001 From: christolis Date: Fri, 27 Jun 2025 13:44:16 +0300 Subject: [PATCH] fix(logger): friendlier handling of default values I have noticed that when inexperienced people just getting started on the project go through all the hoops of cloning the repository, setting up their Discord bots, creating their test server, they end up compiling the code in IDEs like IntelliJ IDEA and assuming they just change the token on their `config.json`, there are stacktraces and errors thrown once they attempt to run the bot. Specifically: Caused by: java.net.URISyntaxException: Illegal character in path at index 0: at java.base/java.net.URI$Parser.fail(URI.java:2995) at java.base/java.net.URI$Parser.checkChars(URI.java:3166) at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3248) at java.base/java.net.URI$Parser.parse(URI.java:3207) at java.base/java.net.URI.(URI.java:645) at java.base/java.net.URI.create(URI.java:930) For the average software engineer, they would be aware that the program is still running and that the bot is ready, but two things: - Someone who is not trained enough and is just starting to contribute will get thrown off by the error and potentially give up (I have experienced it happening) - It's a generally better experience for knowledgable new contributors to set up the project effortlessly so that they can get started with contributing straight away without any stacktraces. This commit prints a friendly warning in the logger and no stacktrace in case the user has not updated the webhooks (which most of them will not have to, as they are primarily concerned with the Discord bot's token). We hide the stacktrace in case there is a "" value in the "logInfoChannelWebhook" and "logErrorChannelWebhook" configuration keys so that IntelliJ IDEA does not put the stacktrace in the spotlight and potentially scare new contributors. Signed-off-by: christolis --- .../tjbot/logging/discord/DiscordLogging.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) 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 99d2de7970..f7afed7fde 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 @@ -23,6 +23,7 @@ */ public final class DiscordLogging { private static final Logger logger = LoggerFactory.getLogger(DiscordLogging.class); + private static final String LOG_CHANNEL_WEBHOOK_DEFAULT_VALUE = ""; private DiscordLogging() { throw new UnsupportedOperationException("Utility class"); @@ -45,22 +46,29 @@ public static void startDiscordLogging(Config botConfig) { } private static void addAppenders(Configuration logConfig, Config botConfig) { - parseWebhookUri(botConfig.getLogInfoChannelWebhook()) + parseWebhookUri(botConfig.getLogInfoChannelWebhook(), "info") .ifPresent(webhookUri -> addDiscordLogAppender("DiscordInfo", createInfoRangeFilter(), webhookUri, botConfig.getSourceCodeBaseUrl(), logConfig)); - parseWebhookUri(botConfig.getLogErrorChannelWebhook()) + parseWebhookUri(botConfig.getLogErrorChannelWebhook(), "error") .ifPresent(webhookUri -> addDiscordLogAppender("DiscordError", createErrorRangeFilter(), webhookUri, botConfig.getSourceCodeBaseUrl(), logConfig)); } - private static Optional parseWebhookUri(String webhookUri) { + private static Optional parseWebhookUri(String webhookUri, String webhookName) { + if (webhookUri.equalsIgnoreCase(LOG_CHANNEL_WEBHOOK_DEFAULT_VALUE)) { + logger.warn( + "The {} webhook URL was not setup yet, logs will not be forwarded to Discord. Enter a valid webhook URL in your `config.json` if you want log forwarding.", + webhookName); + return Optional.empty(); + } + try { return Optional.of(URI.create(webhookUri)); } catch (IllegalArgumentException e) { logger.warn(LogMarkers.NO_DISCORD, - "The webhook URL ({}) in the config is invalid, logs will not be forwarded to Discord.", - webhookUri, e); + "The {} webhook URL ({}) in the config is invalid, logs will not be forwarded to Discord.", + webhookName, webhookUri, e); return Optional.empty(); } }