From 6bbaf3fe7e2727c5ab8b2fe8816d41408bfa8251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 15 Jan 2023 01:48:56 +0100 Subject: [PATCH] Deprecate logger and start moving to SLF4J JDA already has SLF4J as a requirement, so we might as well use that instead of making our own. --- .../hidekobot/commands/base/Trivia.java | 4 +- .../hidekobot/datasources/DatabaseSource.java | 58 ++++++++++--------- .../hidekobot/runnables/HeartBeatTask.java | 15 ++--- .../wtf/beatrice/hidekobot/util/Logger.java | 1 + 4 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java index db5a6c0..e012366 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java @@ -11,6 +11,7 @@ import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu; import org.apache.commons.text.StringEscapeUtils; import org.json.JSONArray; import org.json.JSONObject; +import org.slf4j.LoggerFactory; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.objects.MessageResponse; import wtf.beatrice.hidekobot.objects.comparators.TriviaCategoryComparator; @@ -34,6 +35,7 @@ import java.util.concurrent.TimeUnit; public class Trivia { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Trivia.class); private static final String triviaLink = "https://opentdb.com/api.php?amount=10&type=multiple&category="; private static final String categoriesLink = "https://opentdb.com/api_category.php"; @@ -105,7 +107,7 @@ public class Trivia bufferedReader.close(); return new JSONObject(jsonStrBuilder.toString()); } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("JSON Parsing Exception", e); } return null; diff --git a/src/main/java/wtf/beatrice/hidekobot/datasources/DatabaseSource.java b/src/main/java/wtf/beatrice/hidekobot/datasources/DatabaseSource.java index fb82d71..6e75588 100644 --- a/src/main/java/wtf/beatrice/hidekobot/datasources/DatabaseSource.java +++ b/src/main/java/wtf/beatrice/hidekobot/datasources/DatabaseSource.java @@ -3,8 +3,8 @@ package wtf.beatrice.hidekobot.datasources; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.ChannelType; +import org.slf4j.LoggerFactory; import wtf.beatrice.hidekobot.Cache; -import wtf.beatrice.hidekobot.util.Logger; import java.sql.*; import java.time.LocalDateTime; @@ -15,15 +15,19 @@ import java.util.List; public class DatabaseSource { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DatabaseSource.class); private static final String sqliteURL = "jdbc:sqlite:%path%"; private Connection dbConnection = null; private final String dbPath; - private final Logger logger; public DatabaseSource(String dbPath) { this.dbPath = dbPath; - this.logger = new Logger(getClass()); + } + + private void logException(SQLException e) + { + LOGGER.error("Database Exception", e); } public boolean connect() @@ -34,10 +38,10 @@ public class DatabaseSource try { dbConnection = DriverManager.getConnection(url); - logger.log("Database connection established!"); + LOGGER.info("Database connection established!"); return true; } catch (SQLException e) { - e.printStackTrace(); + logException(e); return false; } @@ -54,7 +58,7 @@ public class DatabaseSource dbConnection.close(); } } catch (SQLException e) { - e.printStackTrace(); + logException(e); return false; } @@ -126,7 +130,7 @@ public class DatabaseSource // execute the statement stmt.execute(sql); } catch (SQLException e) { - e.printStackTrace(); + logException(e); return false; } } @@ -168,7 +172,7 @@ public class DatabaseSource return true; } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return false; @@ -199,7 +203,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -223,7 +227,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -264,7 +268,7 @@ public class DatabaseSource return true; } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return false; @@ -286,7 +290,7 @@ public class DatabaseSource messages.add(resultSet.getString("message_id")); } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return messages; @@ -302,7 +306,7 @@ public class DatabaseSource preparedStatement.execute(); } catch (SQLException e) { - e.printStackTrace(); + logException(e); return false; } @@ -313,7 +317,7 @@ public class DatabaseSource preparedStatement.execute(); } catch (SQLException e) { - e.printStackTrace(); + logException(e); return false; } @@ -324,7 +328,7 @@ public class DatabaseSource preparedStatement.execute(); } catch (SQLException e) { - e.printStackTrace(); + logException(e); return false; } @@ -348,7 +352,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -371,7 +375,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -394,7 +398,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -424,7 +428,7 @@ public class DatabaseSource return true; } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return false; @@ -447,7 +451,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return 0; @@ -470,7 +474,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -493,7 +497,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -516,7 +520,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -539,7 +543,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -562,7 +566,7 @@ public class DatabaseSource } } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return null; @@ -583,7 +587,7 @@ public class DatabaseSource return true; } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return false; @@ -609,7 +613,7 @@ public class DatabaseSource return true; } catch (SQLException e) { - e.printStackTrace(); + logException(e); } return false; diff --git a/src/main/java/wtf/beatrice/hidekobot/runnables/HeartBeatTask.java b/src/main/java/wtf/beatrice/hidekobot/runnables/HeartBeatTask.java index b3855a8..495ab58 100644 --- a/src/main/java/wtf/beatrice/hidekobot/runnables/HeartBeatTask.java +++ b/src/main/java/wtf/beatrice/hidekobot/runnables/HeartBeatTask.java @@ -1,7 +1,8 @@ package wtf.beatrice.hidekobot.runnables; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import wtf.beatrice.hidekobot.Cache; -import wtf.beatrice.hidekobot.util.Logger; import java.io.IOException; import java.net.HttpURLConnection; @@ -9,12 +10,8 @@ import java.net.URL; public class HeartBeatTask implements Runnable { - private final Logger logger; + private final Logger LOGGER = LoggerFactory.getLogger(HeartBeatTask.class); - public HeartBeatTask() - { - logger = new Logger(getClass()); - } @@ -37,15 +34,15 @@ public class HeartBeatTask implements Runnable if(200 <= responseCode && responseCode < 300) { // only log ok response codes when verbosity is enabled - if(Cache.isVerbose()) logger.log("Heartbeat response code: " + responseCode); + if(Cache.isVerbose()) LOGGER.info("Heartbeat response code: {}", responseCode); } else { - logger.log("Heartbeat returned problematic response code: " + responseCode); + LOGGER.error("Heartbeat returned problematic response code: {}", responseCode); } } catch (IOException e) { - logger.log("Error while trying to push heartbeat: " + e.getMessage()); + LOGGER.error("Error while trying to push heartbeat", e); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/util/Logger.java b/src/main/java/wtf/beatrice/hidekobot/util/Logger.java index e4b5eee..8d0ee79 100644 --- a/src/main/java/wtf/beatrice/hidekobot/util/Logger.java +++ b/src/main/java/wtf/beatrice/hidekobot/util/Logger.java @@ -5,6 +5,7 @@ import java.time.format.DateTimeFormatter; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +@Deprecated public class Logger {