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
|
|
|
|
{
|
|
|
|
|
|
|
|
// 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:13:39 +02:00
|
|
|
System.out.println(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...
|
2022-08-25 22:37:32 +02:00
|
|
|
Executors.newSingleThreadScheduledExecutor().schedule(new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2022-08-25 22:43:04 +02:00
|
|
|
// log the message
|
2022-08-25 22:37:32 +02:00
|
|
|
log(message);
|
|
|
|
}
|
2022-08-25 22:43:04 +02:00
|
|
|
//... after waiting X seconds.
|
2022-08-25 22:37:32 +02:00
|
|
|
}, delay, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}
|
2022-08-25 22:13:39 +02:00
|
|
|
}
|