make better console logging like the old js chomens bot

This commit is contained in:
Chayapak 2023-04-22 12:53:46 +07:00
parent 34250d2bad
commit 9956d3673a
3 changed files with 40 additions and 8 deletions

View file

@ -16,7 +16,7 @@ public class ConsoleCommandContext extends CommandContext {
@Override
public void sendOutput (Component component) {
final String message = ComponentUtilities.stringifyAnsi(component);
bot.logger().log(message);
bot.logger().info(message);
}
@Override

View file

@ -72,11 +72,11 @@ public class ConsolePlugin {
if (commandName.equals("csvr") || commandName.equals("consoleserver")) {
for (Bot bot : allBots) {
if (args.length == 0) {
bot.logger().log("No server specified");
bot.logger().info("No server specified");
return;
}
consoleServer = args[0];
bot.logger().log("Set the console server to " + consoleServer);
bot.logger().info("Set the console server to " + consoleServer);
}
}

View file

@ -7,19 +7,25 @@ import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.Logger;
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LoggerPlugin extends ChatPlugin.ChatListener {
private final Bot bot;
private boolean addedListener = false;
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
public LoggerPlugin(Bot bot) {
this.bot = bot;
bot.addListener(new SessionAdapter() {
@Override
public void connected (ConnectedEvent event) {
log("Successfully connected to: " + bot.host() + ":" + bot.port());
info("Successfully connected to: " + bot.host() + ":" + bot.port());
if (addedListener) return;
bot.chat().addListener(LoggerPlugin.this);
@ -29,18 +35,38 @@ public class LoggerPlugin extends ChatPlugin.ChatListener {
@Override
public void disconnected (DisconnectedEvent event) {
final String reason = ComponentUtilities.stringifyAnsi(event.getReason());
log("Disconnected from " + bot.host() + ":" + bot.port() + ", reason: " + reason);
info("Disconnected from " + bot.host() + ":" + bot.port() + ", reason: " + reason);
}
});
}
public void log (String message) {
// ported from chomens bot js
private String prefix (Component prefix, String _message) {
LocalDateTime dateTime = LocalDateTime.now();
final Component message = Component.translatable(
"[%s %s] [%s] %s",
Component.text(dateTime.format(dateTimeFormatter)).color(NamedTextColor.GRAY),
prefix,
Component.text(bot.host() + ":" + bot.port()).color(NamedTextColor.GRAY),
Component.text(_message).color(NamedTextColor.WHITE)
).color(NamedTextColor.DARK_GRAY);
return ComponentUtilities.stringifyAnsi(message);
}
public void log (String _message) {
final String message = prefix(Component.text("LOG").color(NamedTextColor.GOLD), _message);
bot.console().reader().printAbove(message);
final String formattedMessage = String.format(
"[%s] %s",
bot.host() + ":" + bot.port(),
message
_message
);
bot.console().reader().printAbove(formattedMessage);
Logger.log(
formattedMessage.replaceAll( // use replaceAll for regexes, use replace for normal string
"\u001B\\[[;\\d]*[ -/]*[@-~]",
@ -49,6 +75,12 @@ public class LoggerPlugin extends ChatPlugin.ChatListener {
);
}
public void info (String _message) {
final String message = prefix(Component.text("INFO").color(NamedTextColor.GREEN), _message);
bot.console().reader().printAbove(message);
}
@Override
public void systemMessageReceived(String message, Component component) {
final String ansiMessage = ComponentUtilities.stringifyAnsi(component);