feat: 添加 PluginLogger 工具类
This commit is contained in:
@@ -33,6 +33,7 @@ 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;
|
||||
|
||||
public class HamsterCorePlugin extends JavaPlugin {
|
||||
@@ -87,8 +88,7 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
CoreBukkitAPI.init(configFile);
|
||||
logger.info("已初始化 CoreAPI");
|
||||
} catch (Exception e) {
|
||||
logger.warning("初始化 CoreAPI 出错");
|
||||
e.printStackTrace();
|
||||
logger.log(Level.WARNING, "初始化 CoreAPI 出错", e);
|
||||
}
|
||||
CoreMessage.init(this);
|
||||
logger.info("已初始化语言文本");
|
||||
|
@@ -14,6 +14,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Getter
|
||||
public enum CoreMessage {
|
||||
@@ -70,7 +71,6 @@ public enum CoreMessage {
|
||||
this.message = new DisplayMessage().setMessage(message);
|
||||
}
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public static void init(@NotNull Plugin plugin) {
|
||||
File dataFolder = plugin.getDataFolder();
|
||||
if (dataFolder.mkdirs()) {
|
||||
@@ -82,8 +82,7 @@ public enum CoreMessage {
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("加载消息配置文件时出现了一个异常:");
|
||||
e.printStackTrace();
|
||||
plugin.getLogger().log(Level.WARNING, "加载消息配置文件时出现了一个异常:", e);
|
||||
}
|
||||
}
|
||||
for (CoreMessage value : values()) {
|
||||
@@ -96,15 +95,13 @@ public enum CoreMessage {
|
||||
try {
|
||||
value.message = CoreBukkitUtils.loadDisplayMessage(section);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("加载消息设置 " + value.name() + " 时遇到了一个异常: ");
|
||||
e.printStackTrace();
|
||||
plugin.getLogger().log(Level.WARNING, "加载消息设置 " + value.name() + " 时遇到了一个异常: ", e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
config.save(file);
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().warning("保存消息配置文件时出现了一个异常:");
|
||||
e.printStackTrace();
|
||||
plugin.getLogger().log(Level.WARNING, "保存消息配置文件时出现了一个异常:", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,100 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.bukkit.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 BukkitPluginLogger {
|
||||
@NotNull
|
||||
private final Plugin plugin;
|
||||
@NotNull
|
||||
private final Logger logger;
|
||||
|
||||
public BukkitPluginLogger(@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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user