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. * 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 * @since 1-alpha0
*/ */
private boolean debugEvents; private boolean debugEvents;
@ -184,6 +184,20 @@ public final class EngineConfiguration implements SubsystemConfiguration {
*/ */
private boolean loggerForceStandardOutput; 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. * Constructor.
* *
@ -231,6 +245,8 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "loggerTemplate" -> loggerTemplate = parser.getString(group + property); case "loggerTemplate" -> loggerTemplate = parser.getString(group + property);
case "loggerImmediateShutdown" -> loggerImmediateShutdown = parser.getBoolean(group + property); case "loggerImmediateShutdown" -> loggerImmediateShutdown = parser.getBoolean(group + property);
case "loggerForceStandardOutput" -> loggerForceStandardOutput = parser.getBoolean(group + property); case "loggerForceStandardOutput" -> loggerForceStandardOutput = parser.getBoolean(group + property);
case "optimizeLogging" -> optimizeLogging = parser.getBoolean(group + property);
} }
} catch (NullPointerException ignored) {} } 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>"; loggerTemplate = "%log_color_primary%[%time_hour%:%time_minute%:%time_second%] [%log_level% %log_path%%log_info%] %log_color_secondary%%log_message%<reset>";
loggerImmediateShutdown = false; loggerImmediateShutdown = false;
loggerForceStandardOutput = false; loggerForceStandardOutput = false;
optimizeLogging = false;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -274,9 +292,11 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "debugShortcodeConverter" -> { case "debugShortcodeConverter" -> {
return debugShortcodeConverter; return debugShortcodeConverter;
} }
case "errorShortcodeConverter" -> { case "errorShortcodeConverter" -> {
return errorShortcodeConverter; return errorShortcodeConverter;
} }
case "loggerLevel" -> { case "loggerLevel" -> {
return loggerLevel; return loggerLevel;
} }
@ -289,6 +309,10 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "loggerForceStandardOutput" -> { case "loggerForceStandardOutput" -> {
return loggerForceStandardOutput; return loggerForceStandardOutput;
} }
case "optimizeLogging" -> {
return optimizeLogging;
}
default -> { default -> {
return null; return null;
} }

View file

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