Fix logger timestamp not showing time
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-08-25 22:43:04 +02:00
parent 188eafa2ee
commit 94c126e32b
2 changed files with 12 additions and 7 deletions

View File

@ -46,8 +46,8 @@ public class HidekoBot
standardInviteLink = standardInviteLink.replace("%userid%", botUserId); standardInviteLink = standardInviteLink.replace("%userid%", botUserId);
// log the invite-link to console so noob users can just click on it. // log the invite-link to console so noob users can just click on it.
logger.log("Bot User ID: " + botUserId, 10); logger.log("Bot User ID: " + botUserId, 5);
logger.log("Invite Link: " + standardInviteLink, 10); logger.log("Invite Link: " + standardInviteLink, 5);
} }

View File

@ -3,7 +3,6 @@ package wtf.beatrice.hidekobot.utils;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class Logger public class Logger
@ -11,8 +10,9 @@ public class Logger
// objects that we need to have for a properly formatted message // objects that we need to have for a properly formatted message
private String className; private String className;
private final String format = "[%date%] [%class%] %message%"; private final String format = "[%date% %time%] [%class%] %message%";
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd"); 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 // when initializing a new logger, save variables in that instance
@ -25,9 +25,11 @@ public class Logger
public void log(String message) public void log(String message)
{ {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
String currentTime = formatter.format(now); String currentDate = dateFormatter.format(now);
String currentTime = timeFormatter.format(now);
System.out.println(format System.out.println(format
.replace("%date%", currentTime) .replace("%date%", currentDate)
.replace("%time%", currentTime)
.replace("%class%", className) .replace("%class%", className)
.replace("%message%", message)); .replace("%message%", message));
} }
@ -35,13 +37,16 @@ public class Logger
// log a message to console after delaying it (in seconds). // log a message to console after delaying it (in seconds).
public void log(String message, int delay) public void log(String message, int delay)
{ {
// create a new scheduled executor with an anonymous runnable...
Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() Executors.newSingleThreadScheduledExecutor().schedule(new Runnable()
{ {
@Override @Override
public void run() public void run()
{ {
// log the message
log(message); log(message);
} }
//... after waiting X seconds.
}, delay, TimeUnit.SECONDS); }, delay, TimeUnit.SECONDS);
} }