Make logger (optionally) asynchronous!

Finally! Multithreading!
This commit is contained in:
JeremyStar™ 2024-06-11 21:49:48 +02:00
parent 0a6ed79673
commit b0d740cd65
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 60 additions and 30 deletions

View file

@ -85,7 +85,7 @@ public final class EngineConfiguration implements SubsystemConfiguration {
/**
* If enabled, all called events will be logged.
*
* @see de.staropensource.sosengine.base.classes.helpers.EventHelper#logCall(Class)
* @see de.staropensource.sosengine.base.classes.helpers.EventHelper#logCall(Class, Object...)
* @since 1-alpha0
*/
private boolean debugEvents;
@ -184,6 +184,20 @@ public final class EngineConfiguration implements SubsystemConfiguration {
*/
private boolean loggerForceStandardOutput;
/**
* Makes the {@link Logger} work asynchronous if enabled.
*
* @since 1-alpha0
*
* -- GETTER --
* Gets the value for {@code optimizeLogging}.
*
* @return variable value
* @see EngineConfiguration#optimizeLogging
* @since 1-alpha0
*/
private boolean optimizeLogging;
/**
* Constructor.
*
@ -231,6 +245,8 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "loggerTemplate" -> loggerTemplate = parser.getString(group + property);
case "loggerImmediateShutdown" -> loggerImmediateShutdown = parser.getBoolean(group + property);
case "loggerForceStandardOutput" -> loggerForceStandardOutput = parser.getBoolean(group + property);
case "optimizeLogging" -> optimizeLogging = parser.getBoolean(group + property);
}
} catch (NullPointerException ignored) {}
}
@ -259,6 +275,8 @@ public final class EngineConfiguration implements SubsystemConfiguration {
loggerTemplate = "%log_color_primary%[%time_hour%:%time_minute%:%time_second%] [%log_level% %log_path%%log_info%] %log_color_secondary%%log_message%<reset>";
loggerImmediateShutdown = false;
loggerForceStandardOutput = false;
optimizeLogging = false;
}
/** {@inheritDoc} */
@ -274,9 +292,11 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "debugShortcodeConverter" -> {
return debugShortcodeConverter;
}
case "errorShortcodeConverter" -> {
return errorShortcodeConverter;
}
case "loggerLevel" -> {
return loggerLevel;
}
@ -289,6 +309,10 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "loggerForceStandardOutput" -> {
return loggerForceStandardOutput;
}
case "optimizeLogging" -> {
return optimizeLogging;
}
default -> {
return null;
}

View file

@ -86,45 +86,51 @@ public final class Logger {
* @since 1-alpha0
*/
private static void log(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String message) {
// Check if engine has initialized
Runnable loggingCode = () -> {
// Check if engine has initialized
if (Engine.getInstance() == null) return;
if (Engine.getInstance() == null) return;
// Check if level is allowed
if (level.compareTo(EngineConfiguration.getInstance().getLoggerLevel()) < 0)
return;
// Check if level is allowed
if (level.compareTo(EngineConfiguration.getInstance().getLoggerLevel()) < 0)
return;
// Template for now, final log message later
String base = EngineConfiguration.getInstance().getLoggerTemplate();
// Template for now, final log message later
String base = EngineConfiguration.getInstance().getLoggerTemplate();
// Execute LoggerImpl#prePlaceholder
base = loggerImplementation.prePlaceholder(level, logIssuer, base);
// Execute LoggerImpl#prePlaceholder
base = loggerImplementation.prePlaceholder(level, logIssuer, base);
// Create list of temporary placeholders
List<Placeholder> temporaryPlaceholders = new ArrayList<>();
temporaryPlaceholders.add(new LogMessage(message)); // log_message is out of order to allow for placeholder usage
// Create list of temporary placeholders
List<Placeholder> temporaryPlaceholders = new ArrayList<>();
temporaryPlaceholders.add(new LogMessage(message)); // log_message is out of order to allow for placeholder usage
temporaryPlaceholders.add(new LogColorPrimary(level));
temporaryPlaceholders.add(new LogColorSecondary(level));
temporaryPlaceholders.add(new LogClass(logIssuer));
temporaryPlaceholders.add(new LogColorPrimary(level));
temporaryPlaceholders.add(new LogInfo(logIssuer));
temporaryPlaceholders.add(new de.staropensource.sosengine.base.logging.placeholders.logger.LogLevel(level));
temporaryPlaceholders.add(new LogPackage(logIssuer));
temporaryPlaceholders.add(new LogPath(logIssuer));
temporaryPlaceholders.add(new LogColorPrimary(level));
temporaryPlaceholders.add(new LogColorSecondary(level));
temporaryPlaceholders.add(new LogClass(logIssuer));
temporaryPlaceholders.add(new LogColorPrimary(level));
temporaryPlaceholders.add(new LogInfo(logIssuer));
temporaryPlaceholders.add(new de.staropensource.sosengine.base.logging.placeholders.logger.LogLevel(level));
temporaryPlaceholders.add(new LogPackage(logIssuer));
temporaryPlaceholders.add(new LogPath(logIssuer));
// Replace placeholders
base = PlaceholderEngine.getInstance().process(base, temporaryPlaceholders);
// Replace placeholders
base = PlaceholderEngine.getInstance().process(base, temporaryPlaceholders);
// Execute LoggerImpl#postPlaceholder
base = loggerImplementation.postPlaceholder(level, logIssuer, base);
// Execute LoggerImpl#postPlaceholder
base = loggerImplementation.postPlaceholder(level, logIssuer, base);
// Call event
if (!logIssuer.getClazz().getName().equals("de.staropensource.sosengine.slf4j_compat.CompatibilityLogger"))
new LogEvent().callEvent(level, logIssuer, message);
// Call event
if (!logIssuer.getClazz().getName().equals("de.staropensource.sosengine.slf4j_compat.CompatibilityLogger"))
new LogEvent().callEvent(level, logIssuer, message);
// Print log message
loggerImplementation.print(level, logIssuer, base);
};
// Print log message
loggerImplementation.print(level, logIssuer, base);
if (EngineConfiguration.getInstance().isOptimizeLogging())
Thread.ofVirtual().start(loggingCode);
else
loggingCode.run();
}
/**