You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2021/11/21 23:36:02 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-3194 - Allow fractional attributes for size attribute of SizeBsaedTriggeringPolicy.

This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 04506d2  LOG4J2-3194 - Allow fractional attributes for size attribute of SizeBsaedTriggeringPolicy.
04506d2 is described below

commit 04506d259d9eaaa8439536b82b35fd99cb27f38c
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Nov 21 16:35:52 2021 -0700

    LOG4J2-3194 - Allow fractional attributes for size attribute of SizeBsaedTriggeringPolicy.
---
 .../log4j/core/appender/rolling/FileSize.java      | 12 ++++++------
 .../log4j/core/appender/rolling/FileSizeTest.java  | 22 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 +++
 src/site/xdoc/manual/appenders.xml                 |  2 ++
 4 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FileSize.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FileSize.java
index f95dc8e..585f77c 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FileSize.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FileSize.java
@@ -61,20 +61,20 @@ public final class FileSize {
         if (matcher.matches()) {
             try {
                 // Get double precision value
-                final long value = NumberFormat.getNumberInstance(Locale.ROOT).parse(
-                    matcher.group(1)).longValue();
+                final double value = NumberFormat.getNumberInstance(Locale.ROOT).parse(
+                    matcher.group(1)).doubleValue();
 
                 // Get units specified
                 final String units = matcher.group(3);
 
                 if (units.isEmpty()) {
-                    return value;
+                    return (long) value;
                 } else if (units.equalsIgnoreCase("K")) {
-                    return value * KB;
+                    return (long) (value * KB);
                 } else if (units.equalsIgnoreCase("M")) {
-                    return value * MB;
+                    return (long) (value * MB);
                 } else if (units.equalsIgnoreCase("G")) {
-                    return value * GB;
+                    return (long) (value * GB);
                 } else {
                     LOGGER.error("FileSize units not recognized: " + string);
                     return defaultValue;
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
index 35e2199..0723591 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
@@ -17,6 +17,8 @@
 package org.apache.logging.log4j.core.appender.rolling;
 
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 import static org.junit.jupiter.api.Assertions.*;
 
@@ -34,4 +36,24 @@ public class FileSizeTest {
         value = FileSize.parse("10 KB", 0);
         assertEquals(EXPECTED, value, "unexpected value " + value);
     }
+
+    @ParameterizedTest(name = "[{index}] \"{0}\" -> {1}")
+    @CsvSource(delimiter = ':', value = {
+            "10:10",
+            "10KB:10240",
+            "10 KB:10240",
+            "10 kb:10240",
+            " 10 kb :10240",
+            "0.1 MB:104857",
+            "1 MB:1048576",
+            "10 MB:10485760",
+            "10.45 MB:10957619",
+            "10.75 MB:11272192",
+            "1,000 KB:1024000",
+            "1 GB:1073741824",
+            "0.51 GB:547608330"
+    })
+    void testValidFileSizes(String expr, long expected) {
+        assertEquals(expected, FileSize.parse(expr, 0));
+    }
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3d61f8a..dd575a4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,9 @@
     -->
     <release version="2.15.0" date="2021-MM-DD" description="GA Release 2.15.0">
       <!-- ADDS -->
+      <action issue="LOG4J2-3194" dev="rgoers" type="add" due-to="markuss">
+        Allow fractional attributes for size attribute of SizeBsaedTriggeringPolicy.
+      </action>
       <action issue="LOG4J2-2978" dev="rgoers" type="add" due-to="Michael Seele">
         Add support for Jakarta EE 9 (Tomcat 10 / Jetty 11)
       </action>
diff --git a/src/site/xdoc/manual/appenders.xml b/src/site/xdoc/manual/appenders.xml
index 3e663db..31e449a 100644
--- a/src/site/xdoc/manual/appenders.xml
+++ b/src/site/xdoc/manual/appenders.xml
@@ -3219,6 +3219,8 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity {
               <p>
                 The <code>SizeBasedTriggeringPolicy</code> causes a rollover once the file has reached the specified
                 size. The size can be specified in bytes, with the suffix KB, MB or GB, for example <code>20MB</code>.
+                The size may also contain a fractional value such as <code>1.5 MB</code>. The size is evaluated
+                using the Java root Locale so a period must always be used for the fractional unit.
                 When combined with a time based triggering policy the file pattern must contain a <code>%i</code>
                 otherwise the target file will be overwritten on every rollover as the SizeBased Triggering Policy
                 will not cause the timestamp value in the file name to change. When used without a time based