Improve logging when config is missing

This commit is contained in:
floppydiskette 2024-09-05 03:48:06 +01:00
parent 3001616fcb
commit 9b3b3ef320
Signed by: fwoppydwisk
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
2 changed files with 11 additions and 6 deletions

View file

@ -1,6 +1,8 @@
package net.hypr.doki; package net.hypr.doki;
import com.freya02.botcommands.api.Logging;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.slf4j.Logger;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -19,12 +21,15 @@ public class Config {
* @throws IOException if the config JSON could not be read * @throws IOException if the config JSON could not be read
*/ */
public static Config readConfig() throws IOException { public static Config readConfig() throws IOException {
Logger log = Logging.getLogger();
try (InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) { try (InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) {
assert in != null; assert in != null;
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8); Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
log.info("Loaded config");
return new Gson().fromJson(reader, Config.class); return new Gson().fromJson(reader, Config.class);
} catch (IOException | NullPointerException e) { } 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);
} }
} }

View file

@ -13,7 +13,7 @@ import org.slf4j.Logger;
import java.io.IOException; import java.io.IOException;
public class Doki { public class Doki {
private static final Logger LOGGER = Logging.getLogger(); private static final Logger log = Logging.getLogger();
private static JDA jda; private static JDA jda;
private static Config config; private static Config config;
private static final BasicDataSource dataSource = new BasicDataSource(); private static final BasicDataSource dataSource = new BasicDataSource();
@ -50,10 +50,10 @@ public class Doki {
dataSource.setInitialSize(10); dataSource.setInitialSize(10);
//Print some information about the bot //Print some information about the bot
LOGGER.info("Bot connected as {}", jda.getSelfUser().getAsTag()); log.info("Bot connected as {}", jda.getSelfUser().getAsTag());
LOGGER.info("The bot is present on these guilds :"); log.info("The bot is present in the following guilds:");
for (Guild guild : jda.getGuildCache()) { for (Guild guild : jda.getGuildCache()) {
LOGGER.info("\t- {} ({})", guild.getName(), guild.getId()); log.info("\t- {} ({})", guild.getName(), guild.getId());
} }
new Doki(jda, config); 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 .build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib
jda.addEventListener(new LevellingListener()); jda.addEventListener(new LevellingListener());
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Unable to start the bot", e); log.error("Failed to start the bot", e);
System.exit(-1); System.exit(-1);
} }
} }