From 731ce162ea130bc925557c1f3bb6e06c124c1622 Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Wed, 27 Nov 2024 14:12:11 -0600 Subject: [PATCH] Fixes #4292 - Refactor LoggingFeature to do async logging (#4293) --- .mvn/maven.config | 2 +- .../feature/logging/LoggingFeature.java | 25 ++++- .../feature/logging/LoggingHandler.java | 105 ++++++++++++++++++ .../logging/src/main/java/module-info.java | 2 +- .../feature/logging/LoggingFeatureTest.java | 5 +- 5 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 feature/logging/src/main/java/cloud/piranha/feature/logging/LoggingHandler.java diff --git a/.mvn/maven.config b/.mvn/maven.config index ca9905f00d..202d48fe7e 100644 --- a/.mvn/maven.config +++ b/.mvn/maven.config @@ -1,3 +1,3 @@ --show-version -T -8 \ No newline at end of file +4 \ No newline at end of file diff --git a/feature/logging/src/main/java/cloud/piranha/feature/logging/LoggingFeature.java b/feature/logging/src/main/java/cloud/piranha/feature/logging/LoggingFeature.java index 6369145c56..ee239b8a9e 100644 --- a/feature/logging/src/main/java/cloud/piranha/feature/logging/LoggingFeature.java +++ b/feature/logging/src/main/java/cloud/piranha/feature/logging/LoggingFeature.java @@ -27,12 +27,14 @@ */ package cloud.piranha.feature.logging; -import cloud.piranha.feature.impl.DefaultFeature; +import java.util.logging.ConsoleHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; +import cloud.piranha.feature.impl.DefaultFeature; + /** * The Logging feature. * @@ -61,10 +63,29 @@ public String getLevel() { @Override public void init() { + /* + * Remote the default console handler. + */ + Logger rootLogger = Logger.getLogger(""); + Handler[] handlers = rootLogger.getHandlers(); + if (handlers[0] instanceof ConsoleHandler) { + rootLogger.removeHandler(handlers[0]); + } + + /* + * Add our custom console handler. + */ + LoggingHandler loggingHandler = new LoggingHandler(); + rootLogger.addHandler(loggingHandler); + rootLogger.setLevel(Level.ALL); + + /** + * Set the log level (if set). + */ if (level != null) { Logger logger = LogManager.getLogManager().getLogger(""); logger.setLevel(Level.parse(level)); - Handler[] handlers = logger.getHandlers(); + handlers = logger.getHandlers(); for(int i=0; i future = executorService.submit(() -> consoleHandler.publish(record)); + try { + future.get(10, TimeUnit.SECONDS); + } catch (TimeoutException e) { + future.cancel(true); + } catch (Exception e) { + // Handle other exceptions + } + } + } + + @Override + public void flush() { + consoleHandler.flush(); + } + + @Override + public void close() throws SecurityException { + consoleHandler.close(); + executorService.shutdown(); + } +} diff --git a/feature/logging/src/main/java/module-info.java b/feature/logging/src/main/java/module-info.java index cbb1e27716..881bcbaec7 100644 --- a/feature/logging/src/main/java/module-info.java +++ b/feature/logging/src/main/java/module-info.java @@ -36,5 +36,5 @@ exports cloud.piranha.feature.logging; opens cloud.piranha.feature.logging; requires transitive cloud.piranha.feature.impl; - requires java.logging; + requires transitive java.logging; } diff --git a/feature/logging/src/test/java/cloud/piranha/feature/logging/LoggingFeatureTest.java b/feature/logging/src/test/java/cloud/piranha/feature/logging/LoggingFeatureTest.java index d6d30d6617..16d8fad90e 100644 --- a/feature/logging/src/test/java/cloud/piranha/feature/logging/LoggingFeatureTest.java +++ b/feature/logging/src/test/java/cloud/piranha/feature/logging/LoggingFeatureTest.java @@ -27,9 +27,10 @@ */ package cloud.piranha.feature.logging; -import static java.util.logging.Level.INFO; +import static java.util.logging.Level.ALL; import static java.util.logging.Level.SEVERE; import java.util.logging.LogManager; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Test; @@ -59,7 +60,7 @@ void testGetLevel() { void testInit() { LoggingFeature feature = new LoggingFeature(); feature.init(); - assertEquals(INFO, LogManager.getLogManager().getLogger("").getLevel()); + assertEquals(ALL, LogManager.getLogManager().getLogger("").getLevel()); feature = new LoggingFeature(); feature.setLevel("SEVERE"); feature.init();