Skip to content

Commit

Permalink
Merge pull request #9670 from jamezp/issue9259
Browse files Browse the repository at this point in the history
If the rotate-on-boot is defined and there is a file-suffix use the PeriodicSizeRotatingFileHandler
  • Loading branch information
gsmet authored Jun 3, 2020
2 parents 3aa59a8 + 5a4da7e commit 369c6b4
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public static class RotationConfig {

/**
* Indicates whether to rotate log files on server initialization.
* <p>
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ public final class LogConfig {
@ConfigItem(defaultValue = "INFO")
public Level minLevel;

/**
* Console logging.
* <p>
* Console logging is enabled by default.
*/
@ConfigDocSection
public ConsoleConfig console;

/**
* File logging.
* <p>
* Logging to a file is also supported but not enabled by default.
*/
@ConfigDocSection
public FileConfig file;

/**
* Syslog logging.
* <p>
* Logging to a syslog is also supported but not enabled by default.
*/
@ConfigDocSection
public SyslogConfig syslog;

/**
* Logging categories.
* <p>
Expand Down Expand Up @@ -74,30 +98,6 @@ public final class LogConfig {
@ConfigDocSection
public Map<String, SyslogConfig> syslogHandlers;

/**
* Console logging.
* <p>
* Console logging is enabled by default.
*/
@ConfigDocSection
public ConsoleConfig console;

/**
* File logging.
* <p>
* Logging to a file is also supported but not enabled by default.
*/
@ConfigDocSection
public FileConfig file;

/**
* Syslog logging.
* <p>
* Logging to a syslog is also supported but not enabled by default.
*/
@ConfigDocSection
public SyslogConfig syslog;

/**
* Log cleanup filters - internal use.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,12 @@ private static Handler configureFileHandler(final FileConfig config, final Error
final List<LogCleanupFilterElement> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 369c6b4

Please sign in to comment.