From 5f184120555bcb26d44867c6b1153bb4e9970b29 Mon Sep 17 00:00:00 2001 From: Andrus Adamchik Date: Thu, 30 Nov 2023 08:17:33 -0500 Subject: [PATCH] Upgrade to Logback 1.4.13 #66 --- RELEASE-NOTES.md | 4 + .../policy/FixedWindowPolicyFactory.java | 110 ++++++++++-------- .../test-file-appender-fixed-rotation.yml | 1 + pom.xml | 2 +- 4 files changed, 69 insertions(+), 48 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 666009c..243a1b0 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,3 +1,7 @@ +## 3.0-M3 + +* #66 Upgrade to Logback 1.4.13 + ## 3.0.M2 * #63 Upgrade Logback to 1.4.5 diff --git a/bootique-logback/src/main/java/io/bootique/logback/policy/FixedWindowPolicyFactory.java b/bootique-logback/src/main/java/io/bootique/logback/policy/FixedWindowPolicyFactory.java index a7b6ada..70302ff 100644 --- a/bootique-logback/src/main/java/io/bootique/logback/policy/FixedWindowPolicyFactory.java +++ b/bootique-logback/src/main/java/io/bootique/logback/policy/FixedWindowPolicyFactory.java @@ -28,68 +28,84 @@ import com.fasterxml.jackson.annotation.JsonTypeName; import io.bootique.annotation.BQConfig; import io.bootique.annotation.BQConfigProperty; +import io.bootique.value.Duration; /** * A factory that can be used to setup "fixed window" log rolling policy. The * policy is triggered by the main log file reaching a certain size and will * keep up to "historySize" rotated files. Follow the logback link below for * file name pattern rules, etc. - * + * * @see - * Logback documentation + * "http://logback.qos.ch/manual/appenders.html#FixedWindowRollingPolicy"> + * Logback documentation */ @JsonTypeName("fixedWindow") @BQConfig public class FixedWindowPolicyFactory extends RollingPolicyFactory { - private String fileSize; + private String fileSize; + private Duration checkIncrement; + + /** + * Sets a maximum size of a single log file. Exceeding this size causes + * rotation. + * + * @param fileSize maximum size of a single log file expressed in bytes, + * kilobytes, megabytes or gigabytes by suffixing a numeric value + * with KB, MB and respectively GB. For example: 5000000, 5000KB, + * 5MB and 2GB. + */ + // TODO: convert to Bytes value object from String + @BQConfigProperty + public void setFileSize(String fileSize) { + this.fileSize = fileSize; + } + + /** + * @since 3.0 + */ + @BQConfigProperty + public void setCheckIncrement(Duration checkIncrement) { + this.checkIncrement = checkIncrement; + } + + @Override + protected FixedWindowRollingPolicy instantiatePolicy(LoggerContext context) { + FixedWindowRollingPolicy policy = new FixedWindowRollingPolicy(); + policy.setFileNamePattern(getFileNamePattern()); + if (getHistorySize() > 0) { + policy.setMinIndex(1); + policy.setMaxIndex(getHistorySize()); + } + policy.setContext(context); + return policy; + } + + @Override + public TriggeringPolicy createTriggeringPolicy(LoggerContext context) { + SizeBasedTriggeringPolicy policy = new SizeBasedTriggeringPolicy(); - /** - * Sets a maximum size of a single log file. Exceeding this size causes - * rotation. - * - * @param fileSize - * maximum size of a single log file expressed in bytes, - * kilobytes, megabytes or gigabytes by suffixing a numeric value - * with KB, MB and respectively GB. For example: 5000000, 5000KB, - * 5MB and 2GB. - */ - @BQConfigProperty - public void setFileSize(String fileSize) { - this.fileSize = fileSize; - } + if (fileSize != null && fileSize.length() > 0) { + policy.setMaxFileSize(FileSize.valueOf(fileSize)); + } - @Override - protected FixedWindowRollingPolicy instantiatePolicy(LoggerContext context) { - FixedWindowRollingPolicy policy = new FixedWindowRollingPolicy(); - policy.setFileNamePattern(getFileNamePattern()); - if (getHistorySize() > 0) { - policy.setMinIndex(1); - policy.setMaxIndex(getHistorySize()); - } - policy.setContext(context); - return policy; - } + if (checkIncrement != null) { + policy.setCheckIncrement(new ch.qos.logback.core.util.Duration(checkIncrement.getDuration().toMillis())); + } - @Override - public TriggeringPolicy createTriggeringPolicy(LoggerContext context) { - SizeBasedTriggeringPolicy policy = new SizeBasedTriggeringPolicy(); - if (fileSize != null && fileSize.length() > 0) { - policy.setMaxFileSize(FileSize.valueOf(fileSize)); - } - policy.setContext(context); - return policy; - } + policy.setContext(context); + return policy; + } - @Override - protected FileNamePatternValidator getFileNamePatternValidator(LoggerContext context) { + @Override + protected FileNamePatternValidator getFileNamePatternValidator(LoggerContext context) { - return new FileNamePatternValidator(context, getFileNamePattern(), FixedWindowRollingPolicy.class.getSimpleName()) { - @Override - protected void validate() { - checkPattern(false, true); - } - }; - } + return new FileNamePatternValidator(context, getFileNamePattern(), FixedWindowRollingPolicy.class.getSimpleName()) { + @Override + protected void validate() { + checkPattern(false, true); + } + }; + } } diff --git a/bootique-logback/src/test/resources/io/bootique/logback/test-file-appender-fixed-rotation.yml b/bootique-logback/src/test/resources/io/bootique/logback/test-file-appender-fixed-rotation.yml index 5819d69..d1800bf 100644 --- a/bootique-logback/src/test/resources/io/bootique/logback/test-file-appender-fixed-rotation.yml +++ b/bootique-logback/src/test/resources/io/bootique/logback/test-file-appender-fixed-rotation.yml @@ -24,3 +24,4 @@ log: fileNamePattern: "target/logs/logfile-%i.log" historySize: 2 fileSize: 20 + checkIncrement: 50ms diff --git a/pom.xml b/pom.xml index f3d3d5b..53099ce 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ ${project.version} - 1.4.5 + 1.4.13 5.5.2 0.1.5