mirror of
https://github.com/System-End/Discord-MC-Chat.git
synced 2026-04-20 00:25:18 +00:00
finally finish logger part
This commit is contained in:
parent
d1e9d0c449
commit
1e7203ba58
2 changed files with 89 additions and 7 deletions
|
|
@ -17,6 +17,7 @@ dependencies {
|
|||
}
|
||||
|
||||
shadow("org.slf4j:slf4j-api:2.0.17")
|
||||
shadow("org.slf4j:slf4j-jdk14:2.0.17")
|
||||
}
|
||||
|
||||
neoForge {
|
||||
|
|
@ -46,7 +47,9 @@ shadowJar {
|
|||
archiveClassifier.set("")
|
||||
configurations = [project.configurations.shadow]
|
||||
mergeServiceFiles()
|
||||
minimize()
|
||||
minimize {
|
||||
exclude(dependency("org.slf4j:slf4j-jdk14"))
|
||||
}
|
||||
|
||||
exclude "**/*-info.class"
|
||||
exclude "META-INF/maven/"
|
||||
|
|
|
|||
|
|
@ -1,24 +1,103 @@
|
|||
package com.xujiayao.discord_mc_chat.common.utils;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.logging.Formatter;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
public class Logger {
|
||||
|
||||
private final org.slf4j.Logger logger;
|
||||
private final Object logger;
|
||||
|
||||
private final Method infoMethod;
|
||||
private final Method warnMethod;
|
||||
private final Method errorMethod;
|
||||
|
||||
public Logger() {
|
||||
logger = LoggerFactory.getLogger("discord_mc_chat");
|
||||
String loggerClassName = "dmcc_dep.org.slf4j.Logger";
|
||||
String loggerFactoryClassName = "dmcc_dep.org.slf4j.LoggerFactory";
|
||||
|
||||
try {
|
||||
Class<?> loggerClass;
|
||||
|
||||
if (isMinecraftEnvironment()) {
|
||||
loggerClass = Class.forName(loggerClassName.replace("dmcc_dep.", ""));
|
||||
|
||||
Class<?> loggerFactoryClass = Class.forName(loggerFactoryClassName.replace("dmcc_dep.", ""));
|
||||
this.logger = loggerFactoryClass.getMethod("getLogger", String.class).invoke(null, "discord_mc_chat");
|
||||
} else {
|
||||
loggerClass = org.slf4j.Logger.class;
|
||||
|
||||
this.logger = org.slf4j.LoggerFactory.getLogger("discord_mc_chat");
|
||||
|
||||
java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("");
|
||||
for (Handler handler : julLogger.getHandlers()) {
|
||||
handler.setFormatter(new CustomFormatter());
|
||||
}
|
||||
}
|
||||
|
||||
this.infoMethod = loggerClass.getMethod("info", String.class, Object[].class);
|
||||
this.warnMethod = loggerClass.getMethod("warn", String.class, Object[].class);
|
||||
this.errorMethod = loggerClass.getMethod("error", String.class, Object[].class);
|
||||
} catch (NoSuchMethodException | ClassNotFoundException | InvocationTargetException |
|
||||
IllegalAccessException e) {
|
||||
throw new RuntimeException("TODO", e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isMinecraftEnvironment() {
|
||||
// Fabric
|
||||
try {
|
||||
Class.forName("net.fabricmc.loader.api.FabricLoader");
|
||||
return true;
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
}
|
||||
|
||||
// NeoForge
|
||||
try {
|
||||
Class.forName("net.neoforged.fml.loading.FMLLoader");
|
||||
return true;
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void info(String message, Object... args) {
|
||||
logger.info(message, args);
|
||||
try {
|
||||
infoMethod.invoke(logger, message, args);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException("TODO", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void warn(String message, Object... args) {
|
||||
logger.warn(message, args);
|
||||
try {
|
||||
warnMethod.invoke(logger, message, args);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException("TODO", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void error(String message, Object... args) {
|
||||
logger.error(message, args);
|
||||
try {
|
||||
errorMethod.invoke(logger, message, args);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException("TODO", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class CustomFormatter extends Formatter {
|
||||
@Override
|
||||
public String format(LogRecord record) {
|
||||
String time = new SimpleDateFormat("HH:mm:ss").format(System.currentTimeMillis());
|
||||
String thread = Thread.currentThread().getName();
|
||||
String level = record.getLevel().getName().replace("SEVERE", "ERROR");
|
||||
String msg = formatMessage(record);
|
||||
|
||||
return String.format("[%s] [%s/%s]: %s%n", time, thread, level, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue