This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 4485d20 LOG4J2-3303 Add TB support to FileSize. (#671)
4485d20 is described below
commit 4485d209bf3a6532e3faec98bd1652799f793dc6
Author: Volkan Yazici <[email protected]>
AuthorDate: Wed Jan 19 16:25:16 2022 +0100
LOG4J2-3303 Add TB support to FileSize. (#671)
---
.../log4j/core/appender/rolling/FileSizeTest.java | 20 +++-----
.../rolling/action/IfAccumulatedFileSizeTest.java | 10 ++--
.../log4j/core/appender/rolling/FileSize.java | 58 +++++++++++++---------
src/changes/changes.xml | 3 ++
src/site/asciidoc/manual/appenders.adoc | 4 +-
5 files changed, 52 insertions(+), 43 deletions(-)
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
index 0723591..9bed91c 100644
---
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
+++
b/log4j-core-test/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-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java
index 70ff7ba..82a1b92 100644
---
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java
+++
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java
@@ -37,6 +37,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) {
@@ -45,7 +47,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();
@@ -56,7 +58,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();
@@ -67,7 +69,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();
@@ -79,7 +81,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/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 fc3457e..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/src/changes/changes.xml b/src/changes/changes.xml
index 7165070..a7fb356 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -279,6 +279,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/asciidoc/manual/appenders.adoc
b/src/site/asciidoc/manual/appenders.adoc
index a246de6..00087c2 100644
--- a/src/site/asciidoc/manual/appenders.adoc
+++ b/src/site/asciidoc/manual/appenders.adoc
@@ -2771,7 +2771,7 @@ and falls back to Log4J initialization time instead.)
SizeBased Triggering Policy
The `SizeBasedTriggeringPolicy` 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 `20MB`.
+specified in bytes, with the suffix KB, MB, GB, or TB for example `20MB`.
size.The size may also contain a fractional value such as `1.5 MB`. 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 `%i`
@@ -3260,7 +3260,7 @@ the threshold count has been exceeded).
|Parameter Name |Type |Description
|exceeds |String |_Required._ 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 `20MB`.
+the suffix KB, MB, GB, or TB, for example `20MB`.
|nestedConditions |PathCondition[] |An optional set of nested
link:#DeletePathCondition[PathConditions]. If any nested conditions