feat: 完成 SimpleLogger

This commit is contained in:
2024-07-06 13:31:29 +08:00
parent a0ae6ec550
commit b6e6641041
20 changed files with 338 additions and 308 deletions

View File

@@ -3,11 +3,13 @@ package cn.hamster3.mc.plugin.core.common.api;
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory;
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
import cn.hamster3.mc.plugin.core.common.util.SimpleLogger;
import com.google.gson.Gson;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import net.kyori.adventure.platform.AudienceProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import redis.clients.jedis.JedisPool;
import javax.sql.DataSource;
@@ -16,50 +18,62 @@ import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Logger;
@Getter
@SuppressWarnings("unused")
public abstract class CoreAPI {
@Getter
protected static CoreAPI instance;
/**
* Redis 连接池
*/
@Getter
@NotNull
private final JedisPool jedisPool;
/**
* 公用数据库连接池
*/
@Getter
@NotNull
private final HikariDataSource hikariDataSource;
private final boolean enableRedis;
private final boolean enableDatabase;
/**
* 异步线程池
*/
@Getter
@NotNull
private final ExecutorService executorService;
/**
* 调度器线程池
*/
@Getter
@NotNull
private final ScheduledExecutorService scheduledService;
/**
* 公用 Redis 连接池
*/
@Nullable
private JedisPool jedisPool;
/**
* 公用数据库连接池
*/
@Nullable
private HikariDataSource hikariDataSource;
public CoreAPI(@NotNull ConfigSection config) {
SimpleLogger logger = getLogger();
executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
scheduledService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
logger.info("已创建线程池");
getLogger().info("正在创建 Redis 连接池");
jedisPool = new JedisPool(config.getString("redis-url"));
getLogger().info("Redis 连接池创建完成");
ConfigSection datasourceConfig = config.getSection("datasource");
if (datasourceConfig == null) {
throw new IllegalArgumentException("配置文件中未找到 datasource 节点");
enableRedis = config.getBoolean("enable-redis", true);
if (enableRedis) {
logger.info("正在创建 Redis 连接池");
jedisPool = new JedisPool(config.getString("redis-url"));
logger.info("Redis 连接池创建完成");
} else {
logger.info("未启用 Redis 功能");
}
enableDatabase = config.getBoolean("enable-database", true);
if (enableDatabase) {
ConfigSection datasourceConfig = config.getSection("datasource");
if (datasourceConfig == null) {
throw new IllegalArgumentException("配置文件中未找到 datasource 节点");
}
logger.info("正在创建数据库连接池");
hikariDataSource = (HikariDataSource) CoreUtils.getDataSource(datasourceConfig);
logger.info("数据库连接池创建完成");
} else {
logger.info("未启用数据库功能");
}
getLogger().info("正在创建数据库连接池");
hikariDataSource = (HikariDataSource) CoreUtils.getDataSource(datasourceConfig);
getLogger().info("数据库连接池创建完成");
}
/**
@@ -69,7 +83,10 @@ public abstract class CoreAPI {
*/
@NotNull
public DataSource getDataSource() {
return hikariDataSource;
if (hikariDataSource != null) {
return hikariDataSource;
}
throw new IllegalStateException("仓鼠核心未启用数据库功能");
}
/**
@@ -84,7 +101,15 @@ public abstract class CoreAPI {
}
@NotNull
public abstract Logger getLogger();
public JedisPool getJedisPool() {
if (jedisPool != null) {
return jedisPool;
}
throw new IllegalStateException("仓鼠核心未启用 Redis 功能");
}
@NotNull
public abstract SimpleLogger getLogger();
/**
* @return GSON 工具

View File

@@ -0,0 +1,98 @@
package cn.hamster3.mc.plugin.core.common.util;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.logging.Level;
@Getter
@SuppressWarnings("unused")
public abstract class SimpleLogger {
public abstract void log(@NotNull Level level, @NotNull String msg);
public abstract void log(@NotNull Level level, @NotNull Throwable throwable);
public abstract void log(@NotNull Level level, @NotNull Throwable throwable, @NotNull String msg);
public void log(@NotNull Level level, @NotNull String msg, @NotNull Object... args) {
try {
log(level, String.format(msg, args));
} catch (Exception e) {
log(Level.WARNING, "输出日志 " + msg + " 时遇到一个异常");
log(Level.WARNING, "日志参数: " + Arrays.toString(args));
log(Level.WARNING, "异常信息: ", e);
}
}
public void log(@NotNull Level level, @NotNull Throwable throwable, @NotNull String msg, @NotNull Object... args) {
try {
log(level, String.format(msg, args), throwable);
} catch (Exception e) {
log(Level.WARNING, "输出日志 " + msg + " 时遇到一个异常");
log(Level.WARNING, "日志参数: " + Arrays.toString(args));
log(Level.WARNING, "异常参数: ", throwable);
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) {
log(Level.INFO, throwable);
}
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) {
log(Level.WARNING, throwable);
}
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) {
log(Level.SEVERE, throwable);
}
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);
}
}