From 561b73ea6f54d41aa59ad52088dbf79720210b00 Mon Sep 17 00:00:00 2001 From: James Perkins Date: Thu, 28 May 2020 15:34:51 -0700 Subject: [PATCH 1/3] [9259] If the rotate-on-boot is defined and there is a file-suffix use the PeriodicSizeRotatingFileHandler. Fixes #9259 --- .../runtime/logging/LoggingSetupRecorder.java | 6 ++- ...ile-log-rotating-rotate-on-boot.properties | 7 +++ .../quarkus/logging/LoggingTestsHelper.java | 11 +++-- ...icSizeRotatingLoggingRotateOnBootTest.java | 44 +++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 core/test-extension/deployment/src/main/resources/application-periodic-size-file-log-rotating-rotate-on-boot.properties create mode 100644 core/test-extension/deployment/src/test/java/io/quarkus/logging/PeriodicSizeRotatingLoggingRotateOnBootTest.java diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java index 4682cf3d094b6..d2f959e3a3c49 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java @@ -284,10 +284,12 @@ private static Handler configureFileHandler(final FileConfig config, final Error final List filterElements) { FileHandler handler = new FileHandler(); FileConfig.RotationConfig rotationConfig = config.rotation; - if (rotationConfig.maxFileSize.isPresent() && rotationConfig.fileSuffix.isPresent()) { + if ((rotationConfig.maxFileSize.isPresent() || rotationConfig.rotateOnBoot) + && rotationConfig.fileSuffix.isPresent()) { PeriodicSizeRotatingFileHandler periodicSizeRotatingFileHandler = new PeriodicSizeRotatingFileHandler(); periodicSizeRotatingFileHandler.setSuffix(rotationConfig.fileSuffix.get()); - periodicSizeRotatingFileHandler.setRotateSize(rotationConfig.maxFileSize.get().asLongValue()); + rotationConfig.maxFileSize + .ifPresent(memorySize -> periodicSizeRotatingFileHandler.setRotateSize(memorySize.asLongValue())); periodicSizeRotatingFileHandler.setRotateOnBoot(rotationConfig.rotateOnBoot); periodicSizeRotatingFileHandler.setMaxBackupIndex(rotationConfig.maxBackupIndex); handler = periodicSizeRotatingFileHandler; diff --git a/core/test-extension/deployment/src/main/resources/application-periodic-size-file-log-rotating-rotate-on-boot.properties b/core/test-extension/deployment/src/main/resources/application-periodic-size-file-log-rotating-rotate-on-boot.properties new file mode 100644 index 0000000000000..f4609582ecd93 --- /dev/null +++ b/core/test-extension/deployment/src/main/resources/application-periodic-size-file-log-rotating-rotate-on-boot.properties @@ -0,0 +1,7 @@ +quarkus.log.level=INFO +quarkus.log.file.enable=true +quarkus.log.file.level=INFO +quarkus.log.file.format=%d{HH:mm:ss} %-5p [%c{2.}]] (%t) %s%e%n +quarkus.log.file.rotation.file-suffix=.yyyy-MM-dd +quarkus.log.file.rotation.rotate-on-boot=true +quarkus.root.dsa-key-location=/DSAPublicKey.encoded diff --git a/core/test-extension/deployment/src/test/java/io/quarkus/logging/LoggingTestsHelper.java b/core/test-extension/deployment/src/test/java/io/quarkus/logging/LoggingTestsHelper.java index b2d130f214563..b103f2dd1652d 100644 --- a/core/test-extension/deployment/src/test/java/io/quarkus/logging/LoggingTestsHelper.java +++ b/core/test-extension/deployment/src/test/java/io/quarkus/logging/LoggingTestsHelper.java @@ -3,12 +3,14 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; +import java.util.Optional; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; import org.jboss.logmanager.handlers.DelayedHandler; +import org.junit.jupiter.api.Assertions; import io.quarkus.bootstrap.logging.InitialConfigurator; @@ -22,9 +24,10 @@ public static Handler getHandler(Class clazz) { assertThat(Logger.getLogger("").getHandlers()).contains(delayedHandler); assertThat(delayedHandler.getLevel()).isEqualTo(Level.ALL); - Handler handler = Arrays.stream(delayedHandler.getHandlers()).filter(h -> (clazz.isInstance(h))) - .findFirst().get(); - assertThat(handler).isNotNull(); - return handler; + Optional handler = Arrays.stream(delayedHandler.getHandlers()).filter(h -> (clazz.isInstance(h))) + .findFirst(); + Assertions.assertTrue(handler.isPresent(), () -> String.format("Could not find handler of type %s: %s", clazz, + Arrays.asList(delayedHandler.getHandlers()))); + return handler.get(); } } diff --git a/core/test-extension/deployment/src/test/java/io/quarkus/logging/PeriodicSizeRotatingLoggingRotateOnBootTest.java b/core/test-extension/deployment/src/test/java/io/quarkus/logging/PeriodicSizeRotatingLoggingRotateOnBootTest.java new file mode 100644 index 0000000000000..d5b0e6f82d655 --- /dev/null +++ b/core/test-extension/deployment/src/test/java/io/quarkus/logging/PeriodicSizeRotatingLoggingRotateOnBootTest.java @@ -0,0 +1,44 @@ +package io.quarkus.logging; + +import static io.quarkus.logging.LoggingTestsHelper.getHandler; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.logging.Formatter; +import java.util.logging.Handler; +import java.util.logging.Level; + +import org.jboss.logmanager.formatters.PatternFormatter; +import org.jboss.logmanager.handlers.PeriodicSizeRotatingFileHandler; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; + +public class PeriodicSizeRotatingLoggingRotateOnBootTest { + + private static final String FILE_NAME = PeriodicSizeRotatingLoggingRotateOnBootTest.class.getSimpleName() + ".log"; + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withConfigurationResource("application-periodic-size-file-log-rotating-rotate-on-boot.properties") + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClass(LoggingTestsHelper.class) + .addAsManifestResource("application.properties", "microprofile-config.properties")) + .setLogFileName(FILE_NAME); + + @Test + public void periodicSizeRotatingConfigurationTest() { + Handler handler = getHandler(PeriodicSizeRotatingFileHandler.class); + assertThat(handler.getLevel()).isEqualTo(Level.INFO); + + Formatter formatter = handler.getFormatter(); + assertThat(formatter).isInstanceOf(PatternFormatter.class); + PatternFormatter patternFormatter = (PatternFormatter) formatter; + assertThat(patternFormatter.getPattern()).isEqualTo("%d{HH:mm:ss} %-5p [%c{2.}]] (%t) %s%e%n"); + + PeriodicSizeRotatingFileHandler periodicSizeRotatingFileHandler = (PeriodicSizeRotatingFileHandler) handler; + assertThat(periodicSizeRotatingFileHandler.isRotateOnBoot()).isTrue(); + } +} From d97df4e6d5812798b50acfd393c7bf31a75b5ecd Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 2 Jun 2020 17:25:02 +0200 Subject: [PATCH 2/3] Reorder the log config properties to tweak the doc reference --- .../io/quarkus/runtime/logging/LogConfig.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/LogConfig.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/LogConfig.java index ebc5841e94109..1e5a257ee0d42 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/logging/LogConfig.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/LogConfig.java @@ -36,6 +36,30 @@ public final class LogConfig { @ConfigItem(defaultValue = "INFO") public Level minLevel; + /** + * Console logging. + *

+ * Console logging is enabled by default. + */ + @ConfigDocSection + public ConsoleConfig console; + + /** + * File logging. + *

+ * Logging to a file is also supported but not enabled by default. + */ + @ConfigDocSection + public FileConfig file; + + /** + * Syslog logging. + *

+ * Logging to a syslog is also supported but not enabled by default. + */ + @ConfigDocSection + public SyslogConfig syslog; + /** * Logging categories. *

@@ -74,30 +98,6 @@ public final class LogConfig { @ConfigDocSection public Map syslogHandlers; - /** - * Console logging. - *

- * Console logging is enabled by default. - */ - @ConfigDocSection - public ConsoleConfig console; - - /** - * File logging. - *

- * Logging to a file is also supported but not enabled by default. - */ - @ConfigDocSection - public FileConfig file; - - /** - * Syslog logging. - *

- * Logging to a syslog is also supported but not enabled by default. - */ - @ConfigDocSection - public SyslogConfig syslog; - /** * Log cleanup filters - internal use. */ From 5a4da7ef8250236fc2c81d58594514e086284dad Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 2 Jun 2020 17:26:29 +0200 Subject: [PATCH 3/3] Add some javadoc comment about rotate-on-boot behavior --- .../src/main/java/io/quarkus/runtime/logging/FileConfig.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/FileConfig.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/FileConfig.java index 58ed4b16893be..40f7ec711acde 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/logging/FileConfig.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/FileConfig.java @@ -74,6 +74,8 @@ public static class RotationConfig { /** * Indicates whether to rotate log files on server initialization. + *

+ * You need to either set a {@code max-file-size} or configure a {@code file-suffix} for it to work. */ @ConfigItem(defaultValue = "true") boolean rotateOnBoot;