Skip to content

Commit

Permalink
LOG4J2-3194 - Allow fractional attributes for size attribute of SizeB…
Browse files Browse the repository at this point in the history
…saedTriggeringPolicy.
  • Loading branch information
rgoers committed Nov 21, 2021
1 parent 24ec759 commit 04506d2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ public static long parse(final String string, final long defaultValue) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -34,4 +36,24 @@ public void testFileSize() {
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));
}
}
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions src/site/xdoc/manual/appenders.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 04506d2

Please sign in to comment.