Deprecate logger and start moving to SLF4J
continuous-integration/drone/push Build is passing Details

JDA already has SLF4J as a requirement, so we might as well use that instead of making our own.
This commit is contained in:
Bea 2023-01-15 01:48:56 +01:00
parent 95b4f81235
commit 6bbaf3fe7e
4 changed files with 41 additions and 37 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}

View File

@ -5,6 +5,7 @@ import java.time.format.DateTimeFormatter;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Deprecated
public class Logger
{