add netty logger

This commit is contained in:
Sen 2025-03-29 09:44:56 +01:00
parent 966a16ec25
commit 1b9ef61599
3 changed files with 179 additions and 2 deletions

View file

@ -92,6 +92,7 @@ import game.item.ItemStack;
import game.log.Log; import game.log.Log;
import game.log.LogLevel; import game.log.LogLevel;
import game.log.Message; import game.log.Message;
import game.log.NettyLogger;
import game.material.Material; import game.material.Material;
import game.model.ModelManager; import game.model.ModelManager;
import game.network.IThreadListener; import game.network.IThreadListener;
@ -2759,6 +2760,7 @@ public class Game implements IThreadListener {
timer.setDaemon(true); timer.setDaemon(true);
timer.start(); timer.start();
System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv4Stack", "true");
NettyLogger.init();
Registry.register(); Registry.register();
// game = new Game(); // game = new Game();
Runtime.getRuntime().addShutdownHook(new Thread("Game Shutdown Thread") { Runtime.getRuntime().addShutdownHook(new Thread("Game Shutdown Thread") {

View file

@ -16,7 +16,8 @@ public enum Log {
CONSOLE("Console"), CONSOLE("Console"),
TICK("Tick"), TICK("Tick"),
SOUND("Sound"), SOUND("Sound"),
JNI("JNI"); JNI("JNI"),
NETTY("Netty");
private static class LogMessage { private static class LogMessage {
private final LogLevel level; private final LogLevel level;
@ -110,7 +111,11 @@ public enum Log {
} }
} }
private void log(LogLevel level, String msg) { public static LogLevel getLevel() {
return Game.getGame().level;
}
public void log(LogLevel level, String msg) {
if(level.ordinal() > Game.getGame().level.ordinal()) if(level.ordinal() > Game.getGame().level.ordinal())
return; return;
String prefix = String.format(TextColor.DGRAY + "[" + TextColor.GREEN + "%s" + TextColor.DGRAY + "][" + TextColor.LGRAY + "%s" + TextColor.DGRAY + "|" + TextColor.LGRAY + "%s" + String prefix = String.format(TextColor.DGRAY + "[" + TextColor.GREEN + "%s" + TextColor.DGRAY + "][" + TextColor.LGRAY + "%s" + TextColor.DGRAY + "|" + TextColor.LGRAY + "%s" +

View file

@ -0,0 +1,170 @@
package game.log;
import java.io.PrintWriter;
import java.io.StringWriter;
import io.netty.util.internal.logging.AbstractInternalLogger;
import io.netty.util.internal.logging.FormattingTuple;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.MessageFormatter;
public class NettyLogger extends AbstractInternalLogger {
public static void init() {
InternalLoggerFactory.setDefaultFactory(new InternalLoggerFactory() {
protected InternalLogger newInstance(String name) {
return new NettyLogger(name);
}
});
}
private NettyLogger(String name) {
super(name);
}
public boolean isTraceEnabled() {
return Log.getLevel().ordinal() >= LogLevel.TRACE.ordinal();
}
public void trace(String msg) {
this.log(LogLevel.TRACE, msg, null);
}
public void trace(String format, Object arg) {
FormattingTuple ft = MessageFormatter.format(format, arg);
this.log(LogLevel.TRACE, ft.getMessage(), ft.getThrowable());
}
public void trace(String format, Object argA, Object argB) {
FormattingTuple ft = MessageFormatter.format(format, argA, argB);
this.log(LogLevel.TRACE, ft.getMessage(), ft.getThrowable());
}
public void trace(String format, Object... argArray) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
this.log(LogLevel.TRACE, ft.getMessage(), ft.getThrowable());
}
public void trace(String msg, Throwable t) {
this.log(LogLevel.TRACE, msg, t);
}
public boolean isDebugEnabled() {
return Log.getLevel().ordinal() >= LogLevel.DEBUG.ordinal();
}
public void debug(String msg) {
this.log(LogLevel.DEBUG, msg, null);
}
public void debug(String format, Object arg) {
FormattingTuple ft = MessageFormatter.format(format, arg);
this.log(LogLevel.DEBUG, ft.getMessage(), ft.getThrowable());
}
public void debug(String format, Object argA, Object argB) {
FormattingTuple ft = MessageFormatter.format(format, argA, argB);
this.log(LogLevel.DEBUG, ft.getMessage(), ft.getThrowable());
}
public void debug(String format, Object... argArray) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
this.log(LogLevel.DEBUG, ft.getMessage(), ft.getThrowable());
}
public void debug(String msg, Throwable t) {
this.log(LogLevel.DEBUG, msg, t);
}
public boolean isInfoEnabled() {
return Log.getLevel().ordinal() >= LogLevel.INFO.ordinal();
}
public void info(String msg) {
this.log(LogLevel.INFO, msg, null);
}
public void info(String format, Object arg) {
FormattingTuple ft = MessageFormatter.format(format, arg);
this.log(LogLevel.INFO, ft.getMessage(), ft.getThrowable());
}
public void info(String format, Object argA, Object argB) {
FormattingTuple ft = MessageFormatter.format(format, argA, argB);
this.log(LogLevel.INFO, ft.getMessage(), ft.getThrowable());
}
public void info(String format, Object... argArray) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
this.log(LogLevel.INFO, ft.getMessage(), ft.getThrowable());
}
public void info(String msg, Throwable t) {
this.log(LogLevel.INFO, msg, t);
}
public boolean isWarnEnabled() {
return Log.getLevel().ordinal() >= LogLevel.WARN.ordinal();
}
public void warn(String msg) {
this.log(LogLevel.WARN, msg, null);
}
public void warn(String format, Object arg) {
FormattingTuple ft = MessageFormatter.format(format, arg);
this.log(LogLevel.WARN, ft.getMessage(), ft.getThrowable());
}
public void warn(String format, Object argA, Object argB) {
FormattingTuple ft = MessageFormatter.format(format, argA, argB);
this.log(LogLevel.WARN, ft.getMessage(), ft.getThrowable());
}
public void warn(String format, Object... argArray) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
this.log(LogLevel.WARN, ft.getMessage(), ft.getThrowable());
}
public void warn(String msg, Throwable t) {
this.log(LogLevel.WARN, msg, t);
}
public boolean isErrorEnabled() {
return Log.getLevel().ordinal() >= LogLevel.ERROR.ordinal();
}
public void error(String msg) {
this.log(LogLevel.ERROR, msg, null);
}
public void error(String format, Object arg) {
FormattingTuple ft = MessageFormatter.format(format, arg);
this.log(LogLevel.ERROR, ft.getMessage(), ft.getThrowable());
}
public void error(String format, Object argA, Object argB) {
FormattingTuple ft = MessageFormatter.format(format, argA, argB);
this.log(LogLevel.ERROR, ft.getMessage(), ft.getThrowable());
}
public void error(String format, Object... arguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
this.log(LogLevel.ERROR, ft.getMessage(), ft.getThrowable());
}
public void error(String msg, Throwable t) {
this.log(LogLevel.ERROR, msg, t);
}
private void log(LogLevel level, String msg, Throwable t) {
Log.NETTY.log(level, msg);
if(t != null) {
StringWriter sw = new StringWriter();
PrintWriter st = new PrintWriter(sw);
t.printStackTrace(st);
sw.flush();
Log.NETTY.log(level, sw.toString().trim().replace('\t', ' '));
}
}
}