HidekoBot/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java

67 lines
2.7 KiB
Java
Raw Normal View History

2022-08-25 22:13:39 +02:00
package wtf.beatrice.hidekobot.utils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
2022-08-25 22:37:32 +02:00
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
2022-08-25 22:13:39 +02:00
public class Logger
{
2022-08-25 22:54:08 +02:00
// cosmetic string to print on startup.
private String logo =
"██╗░░██╗██╗██████╗░███████╗██╗░░██╗░█████╗░\n" +
"██║░░██║██║██╔══██╗██╔════╝██║░██╔╝██╔══██╗\n" +
"███████║██║██║░░██║█████╗░░█████═╝░██║░░██║\n" +
"██╔══██║██║██║░░██║██╔══╝░░██╔═██╗░██║░░██║\n" +
"██║░░██║██║██████╔╝███████╗██║░╚██╗╚█████╔╝\n" +
"╚═╝░░╚═╝╚═╝╚═════╝░╚══════╝╚═╝░░╚═╝░╚════╝░";
2022-08-25 22:13:39 +02:00
// objects that we need to have for a properly formatted message
private String className;
2022-08-25 22:43:04 +02:00
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");
2022-08-25 22:13:39 +02:00
// when initializing a new logger, save variables in that instance
public Logger(Class logClass)
{
className = logClass.getSimpleName();
}
// log a message to console, with our chosen format
public void log(String message)
{
LocalDateTime now = LocalDateTime.now();
2022-08-25 22:43:04 +02:00
String currentDate = dateFormatter.format(now);
String currentTime = timeFormatter.format(now);
2022-08-25 22:54:08 +02:00
logRaw(format
2022-08-25 22:43:04 +02:00
.replace("%date%", currentDate)
.replace("%time%", currentTime)
2022-08-25 22:13:39 +02:00
.replace("%class%", className)
.replace("%message%", message));
}
2022-08-25 22:37:32 +02:00
// log a message to console after delaying it (in seconds).
public void log(String message, int delay)
{
2022-08-25 22:43:04 +02:00
// create a new scheduled executor with an anonymous runnable...
//... after waiting <delay> seconds.
Executors.newSingleThreadScheduledExecutor().schedule(() -> log(message), delay, TimeUnit.SECONDS);
2022-08-25 22:37:32 +02:00
}
2022-08-25 22:54:08 +02:00
// avoid formatting the text and print whatever is passed.
public void logRaw(String message)
{
System.out.println(message);
}
public String getLogo()
{
return logo;
}
2022-08-25 22:13:39 +02:00
}