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

5
.idea/workspace.xml generated
View File

@@ -5,7 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="997b32da-b4d4-48ac-ab51-52d65f364f81" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/Logging.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/Logging.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/eventbus/GlobalEventBus.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/eventbus/GlobalEventBus.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -81,7 +84,7 @@
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="org.toop.Main" />
<module name="pism" />
<module name="pis" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="org.toop.*" />

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);