From 94c126e32b1a4d42c1f6cbac516a9f6e58782fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Thu, 25 Aug 2022 22:43:04 +0200 Subject: [PATCH] Fix logger timestamp not showing time --- .../java/wtf/beatrice/hidekobot/HidekoBot.java | 4 ++-- .../java/wtf/beatrice/hidekobot/utils/Logger.java | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index e36a80c..046ddcc 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -46,8 +46,8 @@ public class HidekoBot standardInviteLink = standardInviteLink.replace("%userid%", botUserId); // log the invite-link to console so noob users can just click on it. - logger.log("Bot User ID: " + botUserId, 10); - logger.log("Invite Link: " + standardInviteLink, 10); + logger.log("Bot User ID: " + botUserId, 5); + logger.log("Invite Link: " + standardInviteLink, 5); } diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java b/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java index 18db314..38a4b3c 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java @@ -3,7 +3,6 @@ package wtf.beatrice.hidekobot.utils; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Logger @@ -11,8 +10,9 @@ public class Logger // objects that we need to have for a properly formatted message private String className; - private final String format = "[%date%] [%class%] %message%"; - private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd"); + private final String format = "[%date% %time%] [%class%] %message%"; + private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd"); + private final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); // when initializing a new logger, save variables in that instance @@ -25,9 +25,11 @@ public class Logger public void log(String message) { LocalDateTime now = LocalDateTime.now(); - String currentTime = formatter.format(now); + String currentDate = dateFormatter.format(now); + String currentTime = timeFormatter.format(now); System.out.println(format - .replace("%date%", currentTime) + .replace("%date%", currentDate) + .replace("%time%", currentTime) .replace("%class%", className) .replace("%message%", message)); } @@ -35,13 +37,16 @@ public class Logger // log a message to console after delaying it (in seconds). public void log(String message, int delay) { + // create a new scheduled executor with an anonymous runnable... Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() { @Override public void run() { + // log the message log(message); } + //... after waiting X seconds. }, delay, TimeUnit.SECONDS); }