Added the ability to tune the logger

This commit is contained in:
Bas de Jong
2025-09-17 23:40:55 +02:00
parent 26590e1939
commit 0cc57f558d
4 changed files with 69 additions and 18 deletions

View File

@@ -11,19 +11,64 @@ import org.toop.eventbus.EventRegistry;
* Options for logging.
*/
public final class Logging {
public static void disableLogs() {
public static void disableAllLogs() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(EventRegistry.class.getName());
loggerConfig.setLevel(Level.OFF);
ctx.updateLoggers(); // apply changes immediately
}
public static void enableLogs(Level level) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(EventRegistry.class.getName());
loggerConfig.setLevel(level);
LoggerConfig rootLoggerConfig = config.getRootLogger();
rootLoggerConfig.setLevel(Level.OFF);
ctx.updateLoggers();
}
public static void enableAllLogs(Level level) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig rootLoggerConfig = config.getRootLogger();
rootLoggerConfig.setLevel(level);
ctx.updateLoggers();
}
public static <T> void disableLogsForClass(Class<T> class_) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig specificConfig = new LoggerConfig(class_.getName(), Level.OFF, true);
config.addLogger(class_.getName(), specificConfig);
ctx.updateLoggers();
}
public static <T> void enableLogsForClass(Class<T> class_, Level levelToLog) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggers().get(class_.getName());
if (loggerConfig == null) {
loggerConfig = new LoggerConfig(class_.getName(), levelToLog, true);
config.addLogger(class_.getName(), loggerConfig);
} else {
loggerConfig.setLevel(levelToLog);
}
ctx.updateLoggers();
}
public static <T> void enableAllLogsForClass(Class<T> class_) {
enableLogsForClass(class_, Level.ALL);
}
public static <T> void enableDebugLogsForClass(Class<T> class_) {
enableLogsForClass(class_, Level.DEBUG);
}
public static <T> void enableErrorLogsForClass(Class<T> class_) {
enableLogsForClass(class_, Level.ERROR);
}
public static <T> void enableFatalLogsForClass(Class<T> class_) {
enableLogsForClass(class_, Level.FATAL);
}
public static <T> void enableInfoLogsForClass(Class<T> class_) {
enableLogsForClass(class_, Level.INFO);
}
public static <T> void enableTraceLogsForClass(Class<T> class_) {
enableLogsForClass(class_, Level.TRACE);
}
}

View File

@@ -1,6 +1,7 @@
package org.toop;
import org.toop.UI.GameSelectorWindow;
import org.toop.eventbus.EventRegistry;
import org.toop.eventbus.Events;
import org.toop.eventbus.GlobalEventBus;
import org.toop.server.backend.ServerManager;
@@ -16,6 +17,8 @@ public class Main {
private static boolean running = false;
public static void main(String[] args) throws ExecutionException, InterruptedException {
// Logging.disableAllLogs();
// Logging.enableAllLogsForClass(EventRegistry.class);
initSystems();
registerEvents();

View File

@@ -54,9 +54,9 @@ public class GlobalEventBus {
return new Object() {
@Subscribe
public void handle(Object event) {
if (type.isInstance(event)) {
action.accept(type.cast(event));
}
if (type.isInstance(event)) {
action.accept(type.cast(event));
}
}
};
}
@@ -72,9 +72,9 @@ public class GlobalEventBus {
Object listener = new Object() {
@Subscribe
public void handle(Object event) {
if (type.isInstance(event)) {
action.accept(type.cast(event));
}
if (type.isInstance(event)) {
action.accept(type.cast(event));
}
}
};
var re = new EventMeta<>(type, listener);