From 9b3b3ef32004f9043cee20c711bca01bf8dc4339 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Thu, 5 Sep 2024 03:48:06 +0100 Subject: [PATCH] Improve logging when config is missing --- src/main/java/net/hypr/doki/Config.java | 7 ++++++- src/main/java/net/hypr/doki/Doki.java | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/hypr/doki/Config.java b/src/main/java/net/hypr/doki/Config.java index 8585fce..c6b233e 100644 --- a/src/main/java/net/hypr/doki/Config.java +++ b/src/main/java/net/hypr/doki/Config.java @@ -1,6 +1,8 @@ package net.hypr.doki; +import com.freya02.botcommands.api.Logging; import com.google.gson.Gson; +import org.slf4j.Logger; import java.io.*; import java.nio.charset.StandardCharsets; @@ -19,12 +21,15 @@ public class Config { * @throws IOException if the config JSON could not be read */ public static Config readConfig() throws IOException { + Logger log = Logging.getLogger(); try (InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) { assert in != null; Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8); + log.info("Loaded config"); return new Gson().fromJson(reader, Config.class); } catch (IOException | NullPointerException e) { - throw new IOException("config.json was not found, did you forget to create it?", e); + log.error("Failed to load config.json, does the file exist?"); + throw new IOException(e); } } diff --git a/src/main/java/net/hypr/doki/Doki.java b/src/main/java/net/hypr/doki/Doki.java index c0251fb..43d23cf 100644 --- a/src/main/java/net/hypr/doki/Doki.java +++ b/src/main/java/net/hypr/doki/Doki.java @@ -13,7 +13,7 @@ import org.slf4j.Logger; import java.io.IOException; public class Doki { - private static final Logger LOGGER = Logging.getLogger(); + private static final Logger log = Logging.getLogger(); private static JDA jda; private static Config config; private static final BasicDataSource dataSource = new BasicDataSource(); @@ -50,10 +50,10 @@ public class Doki { dataSource.setInitialSize(10); //Print some information about the bot - LOGGER.info("Bot connected as {}", jda.getSelfUser().getAsTag()); - LOGGER.info("The bot is present on these guilds :"); + log.info("Bot connected as {}", jda.getSelfUser().getAsTag()); + log.info("The bot is present in the following guilds:"); for (Guild guild : jda.getGuildCache()) { - LOGGER.info("\t- {} ({})", guild.getName(), guild.getId()); + log.info("\t- {} ({})", guild.getName(), guild.getId()); } new Doki(jda, config); @@ -68,7 +68,7 @@ public class Doki { .build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib jda.addEventListener(new LevellingListener()); } catch (Exception e) { - LOGGER.error("Unable to start the bot", e); + log.error("Failed to start the bot", e); System.exit(-1); } }