You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2022/01/19 15:25:44 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-3303 Add TB support to FileSize. (#671)

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

vy 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 28d6545  LOG4J2-3303 Add TB support to FileSize. (#671)
28d6545 is described below

commit 28d65454e90f733a6fa33312726d9c6dc45325b7
Author: Volkan Yazici <vo...@yazi.ci>
AuthorDate: Wed Jan 19 16:25:16 2022 +0100

    LOG4J2-3303 Add TB support to FileSize. (#671)
---
 .../log4j/core/appender/rolling/FileSize.java      | 58 +++++++++++++---------
 .../log4j/core/appender/rolling/FileSizeTest.java  | 20 +++-----
 .../rolling/action/IfAccumulatedFileSizeTest.java  | 10 ++--
 src/changes/changes.xml                            |  3 ++
 src/site/xdoc/manual/appenders.xml                 |  4 +-
 5 files changed, 52 insertions(+), 43 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 585f77c..fb0aaf6 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
@@ -17,33 +17,34 @@
 
 package org.apache.logging.log4j.core.appender.rolling;
 
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+
 import java.text.NumberFormat;
 import java.text.ParseException;
 import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.status.StatusLogger;
-
 /**
  * FileSize utility class.
  */
 public final class FileSize {
+
     private static final Logger LOGGER = StatusLogger.getLogger();
 
     private static final long KB = 1024;
     private static final long MB = KB * KB;
     private static final long GB = KB * MB;
+    private static final long TB = KB * GB;
 
     /**
      * Pattern for string parsing.
      */
     private static final Pattern VALUE_PATTERN =
-        Pattern.compile("([0-9]+([\\.,][0-9]+)?)\\s*(|K|M|G)B?", Pattern.CASE_INSENSITIVE);
+            Pattern.compile("([0-9]+([.,][0-9]+)?)\\s*(|K|M|G|T)B?", Pattern.CASE_INSENSITIVE);
 
-    private FileSize() {
-    }
+    private FileSize() {}
 
     /**
      * Converts a string to a number of bytes. Strings consist of a floating point value followed by
@@ -60,32 +61,43 @@ public final class FileSize {
         // Valid input?
         if (matcher.matches()) {
             try {
-                // Get double precision value
-                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 (long) value;
-                } else if (units.equalsIgnoreCase("K")) {
-                    return (long) (value * KB);
-                } else if (units.equalsIgnoreCase("M")) {
-                    return (long) (value * MB);
-                } else if (units.equalsIgnoreCase("G")) {
-                    return (long) (value * GB);
+
+                // Read the quantity.
+                final String quantityString = matcher.group(1);
+                final double quantity = NumberFormat
+                        .getNumberInstance(Locale.ROOT)
+                        .parse(quantityString)
+                        .doubleValue();
+
+                // Read the unit.
+                final String unit = matcher.group(3);
+
+                // Calculate the number of bytes.
+                if (unit == null || unit.isEmpty()) {
+                    return (long) quantity;
+                } else if (unit.equalsIgnoreCase("K")) {
+                    return (long) (quantity * KB);
+                } else if (unit.equalsIgnoreCase("M")) {
+                    return (long) (quantity * MB);
+                } else if (unit.equalsIgnoreCase("G")) {
+                    return (long) (quantity * GB);
+                } else if (unit.equalsIgnoreCase("T")) {
+                    return (long) (quantity * TB);
                 } else {
                     LOGGER.error("FileSize units not recognized: " + string);
                     return defaultValue;
                 }
-            } catch (final ParseException e) {
-                LOGGER.error("FileSize unable to parse numeric part: " + string, e);
+
+            } catch (final ParseException error) {
+                LOGGER.error("FileSize unable to parse numeric part: " + string, error);
                 return defaultValue;
             }
         }
+
+        // Invalid input, bail out.
         LOGGER.error("FileSize unable to parse bytes: " + 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 0723591..9bed91c 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
@@ -16,27 +16,16 @@
  */
 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.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
 /**
  * Tests {@link FileSize}.
  */
 public class FileSizeTest {
 
-    private final static long EXPECTED = 10 * 1024;
-
-    @Test
-    public void testFileSize() {
-        long value = FileSize.parse("10KB", 0);
-        assertEquals(EXPECTED, value, "unexpected value " + value);
-        value = FileSize.parse("10 KB", 0);
-        assertEquals(EXPECTED, value, "unexpected value " + value);
-    }
-
     @ParameterizedTest(name = "[{index}] \"{0}\" -> {1}")
     @CsvSource(delimiter = ':', value = {
             "10:10",
@@ -51,9 +40,12 @@ public class FileSizeTest {
             "10.75 MB:11272192",
             "1,000 KB:1024000",
             "1 GB:1073741824",
-            "0.51 GB:547608330"
+            "0.51 GB:547608330",
+            "1 TB:1099511627776",
+            "1023 TB:1124800395214848",
     })
-    void testValidFileSizes(String expr, long expected) {
+    void testValidFileSizes(final String expr, final long expected) {
         assertEquals(expected, FileSize.parse(expr, 0));
     }
+
 }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java
index 76bb0fc..1cc98b2 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java
@@ -36,6 +36,8 @@ public class IfAccumulatedFileSizeTest {
         assertEquals(3 * 1024 * 1024, create("3 MB").getThresholdBytes());
         assertEquals(2L * 1024 * 1024 * 1024, create("2GB").getThresholdBytes());
         assertEquals(3L * 1024 * 1024 * 1024, create("3 GB").getThresholdBytes());
+        assertEquals(2L * 1024 * 1024 * 1024 * 1024, create("2TB").getThresholdBytes());
+        assertEquals(3L * 1024 * 1024 * 1024 * 1024, create("3 TB").getThresholdBytes());
     }
 
     private static IfAccumulatedFileSize create(final String size) {
@@ -44,7 +46,7 @@ public class IfAccumulatedFileSizeTest {
 
     @Test
     public void testNotAcceptOnExactMatch() {
-        final String[] sizes = {"2KB", "3MB", "4GB"};
+        final String[] sizes = {"2KB", "3MB", "4GB", "5TB"};
         for (final String size : sizes) {
             final IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size);
             final DummyFileAttributes attribs = new DummyFileAttributes();
@@ -55,7 +57,7 @@ public class IfAccumulatedFileSizeTest {
 
     @Test
     public void testAcceptIfExceedThreshold() {
-        final String[] sizes = {"2KB", "3MB", "4GB"};
+        final String[] sizes = {"2KB", "3MB", "4GB", "5TB"};
         for (final String size : sizes) {
             final IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size);
             final DummyFileAttributes attribs = new DummyFileAttributes();
@@ -66,7 +68,7 @@ public class IfAccumulatedFileSizeTest {
 
     @Test
     public void testNotAcceptIfBelowThreshold() {
-        final String[] sizes = {"2KB", "3MB", "4GB"};
+        final String[] sizes = {"2KB", "3MB", "4GB", "5TB"};
         for (final String size : sizes) {
             final IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size);
             final DummyFileAttributes attribs = new DummyFileAttributes();
@@ -78,7 +80,7 @@ public class IfAccumulatedFileSizeTest {
     @Test
     public void testAcceptOnceThresholdExceeded() {
         final DummyFileAttributes attribs = new DummyFileAttributes();
-        final String[] sizes = {"2KB", "3MB", "4GB"};
+        final String[] sizes = {"2KB", "3MB", "4GB", "5TB"};
         for (final String size : sizes) {
             final IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size);
             final long quarter = condition.getThresholdBytes() / 4;
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c2fe0e9..9757d13 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -140,6 +140,9 @@
         Fix DTD error: Add missing ELEMENT for Marker.
       </action>
       <!-- ADD -->
+      <action issue="LOG4J2-3303" dev="vy" type="add" due-to="ramananravi">
+        Add TB support to FileSize.
+      </action>
       <action issue="LOG4J2-3282" dev="ckozak" type="add" due-to="Michael Vorburger">
         Add the log4j-to-jul JDK Logging Bridge
       </action>
diff --git a/src/site/xdoc/manual/appenders.xml b/src/site/xdoc/manual/appenders.xml
index 42fb1b8..ecca7a3 100644
--- a/src/site/xdoc/manual/appenders.xml
+++ b/src/site/xdoc/manual/appenders.xml
@@ -3226,7 +3226,7 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity {
             <h5>SizeBased Triggering Policy</h5>
               <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>.
+                size. The size can be specified in bytes, with the suffix KB, MB, GB, or TB, 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>
@@ -3796,7 +3796,7 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity {
               <td>String</td>
               <td><em>Required.</em>
                 The threshold accumulated file size from which files will be deleted.
-                The size can be specified in bytes, with the suffix KB, MB or GB, for example <tt>20MB</tt>.
+                The size can be specified in bytes, with the suffix KB, MB, GB, or TB, for example <tt>20MB</tt>.
               </td>
             </tr>
             <tr>