feat: 添加 PluginLogger 工具类

This commit is contained in:
2024-07-03 18:02:43 +08:00
parent 738b566a2d
commit a0ae6ec550
6 changed files with 209 additions and 13 deletions

View File

@@ -17,9 +17,9 @@ import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.logging.Level;
import java.util.logging.Logger;
@SuppressWarnings("CallToPrintStackTrace")
public class HamsterCorePlugin extends Plugin {
@Getter
private static HamsterCorePlugin instance;
@@ -48,8 +48,7 @@ public class HamsterCorePlugin extends Plugin {
CoreBungeeAPI.init(configFile);
logger.info("已初始化 CoreAPI");
} catch (Exception e) {
logger.warning("初始化 CoreAPI 出错");
e.printStackTrace();
logger.log(Level.WARNING, "初始化 CoreAPI 出错", e);
}
long time = System.currentTimeMillis() - start;
logger.info("仓鼠核心初始化完成,总计耗时 " + time + " ms");

View File

@@ -0,0 +1,100 @@
package cn.hamster3.mc.plugin.core.bungee.util;
import lombok.Getter;
import net.md_5.bungee.api.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
@Getter
@SuppressWarnings("unused")
public class BungeePluginLogger {
@NotNull
private final Plugin plugin;
@NotNull
private final Logger logger;
public BungeePluginLogger(@NotNull Plugin plugin) {
this.plugin = plugin;
logger = plugin.getLogger();
}
public void log(@NotNull Level level, @NotNull String msg) {
logger.log(level, msg);
}
public void log(@NotNull Level level, @NotNull String msg, @NotNull Object... args) {
try {
logger.log(level, String.format(msg, args));
} catch (Exception e) {
logger.log(Level.WARNING, "输出日志 " + msg + " 时遇到一个异常");
logger.log(Level.WARNING, "日志参数: " + Arrays.toString(args));
logger.log(Level.WARNING, "异常信息: ", e);
}
}
public void log(@NotNull Level level, @NotNull Throwable throwable, @NotNull String msg) {
logger.log(level, msg, throwable);
}
public void log(@NotNull Level level, @NotNull Throwable throwable, @NotNull String msg, @NotNull Object... args) {
try {
logger.log(level, String.format(msg, args), throwable);
} catch (Exception e) {
logger.log(Level.WARNING, "输出日志 " + msg + " 时遇到一个异常");
logger.log(Level.WARNING, "日志参数: " + Arrays.toString(args));
logger.log(Level.WARNING, "异常参数: ", throwable);
logger.log(Level.WARNING, "异常信息: ", e);
}
}
public void info(@NotNull String msg) {
log(Level.INFO, msg);
}
public void info(@NotNull String msg, @NotNull Object... args) {
log(Level.INFO, msg, args);
}
public void info(@NotNull Throwable throwable, @NotNull String msg) {
log(Level.INFO, throwable, msg);
}
public void info(@NotNull Throwable throwable, @NotNull String msg, @NotNull Object... args) {
log(Level.INFO, throwable, msg, args);
}
public void warn(@NotNull String msg) {
log(Level.WARNING, msg);
}
public void warn(@NotNull String msg, @NotNull Object... args) {
log(Level.WARNING, msg, args);
}
public void warn(@NotNull Throwable throwable, @NotNull String msg) {
log(Level.WARNING, throwable, msg);
}
public void warn(@NotNull Throwable throwable, @NotNull String msg, @NotNull Object... args) {
log(Level.WARNING, throwable, msg, args);
}
public void error(@NotNull String msg) {
log(Level.SEVERE, msg);
}
public void error(@NotNull String msg, @NotNull Object... args) {
log(Level.SEVERE, msg, args);
}
public void error(@NotNull Throwable throwable, @NotNull String msg) {
log(Level.SEVERE, throwable, msg);
}
public void error(@NotNull Throwable throwable, @NotNull String msg, @NotNull Object... args) {
log(Level.SEVERE, throwable, msg, args);
}
}