You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2024/03/28 15:52:50 UTC
(logging-log4j2) branch remove_duration created (now cd02054cf5)
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a change to branch remove_duration
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
at cd02054cf5 Remove `o.a.l.l.c.a.r.a.Duration` class
This branch includes the following new commits:
new cd02054cf5 Remove `o.a.l.l.c.a.r.a.Duration` class
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(logging-log4j2) 01/01: Remove `o.a.l.l.c.a.r.a.Duration` class
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch remove_duration
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit cd02054cf5be3c11e413f9e26e02d87f5cef63af
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Thu Mar 28 15:03:58 2024 +0100
Remove `o.a.l.l.c.a.r.a.Duration` class
---
.../core/appender/rolling/action/DurationTest.java | 148 ---------------------
.../rolling/action/IfLastModifiedTest.java | 31 +++--
.../appender/rolling/action/IfLastModified.java | 34 +++--
.../config/plugins/convert/CoreTypeConverters.java | 4 +-
src/changelog/.3.x.x/remove_duration.xml | 7 +
5 files changed, 48 insertions(+), 176 deletions(-)
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DurationTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DurationTest.java
deleted file mode 100644
index 75d05c1be8..0000000000
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DurationTest.java
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to you under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.logging.log4j.core.appender.rolling.action;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
-import org.junit.jupiter.api.Test;
-
-/**
- * Tests the Duration class.
- */
-public class DurationTest {
-
- @Test
- public void testParseFailsIfNullText() {
- assertThrows(NullPointerException.class, () -> Duration.parse(null));
- }
-
- @Test
- public void testParseFailsIfInvalidPattern() {
- assertThrows(IllegalArgumentException.class, () -> Duration.parse("abc"));
- }
-
- @Test
- public void testParseFailsIfSectionsOutOfOrder() {
- assertThrows(IllegalArgumentException.class, () -> Duration.parse("P4DT2M1S3H"));
- }
-
- @Test
- public void testParseFailsIfTButMissingTime() {
- assertThrows(IllegalArgumentException.class, () -> Duration.parse("P1dT"));
- }
-
- @Test
- public void testParseIsCaseInsensitive() {
- assertEquals("P4DT3H2M1S", Duration.parse("p4dt3h2m1s").toString());
- }
-
- @Test
- public void testParseAllowsOverflows() {
- assertEquals(1000 * 70, Duration.parse("PT70S").toMillis());
- assertEquals(1000 * 70 * 60, Duration.parse("PT70M").toMillis());
- assertEquals(1000 * 25 * 60 * 60, Duration.parse("PT25H").toMillis());
- }
-
- @Test
- public void testToMillis() {
- assertEquals(0, Duration.ZERO.toMillis());
- assertEquals(1000, Duration.parse("PT1S").toMillis());
- assertEquals(1000 * 2 * 60, Duration.parse("PT2M").toMillis());
- assertEquals(1000 * 3 * 60 * 60, Duration.parse("PT3H").toMillis());
- assertEquals(1000 * 4 * 24 * 60 * 60, Duration.parse("P4D").toMillis());
- final long expected = (1000 * 4 * 24 * 60 * 60) + (1000 * 3 * 60 * 60) + (1000 * 2 * 60) + 1000;
- assertEquals(expected, Duration.parse("P4DT3H2M1S").toMillis());
- }
-
- @Test
- public void testToString() {
- assertEquals("PT0S", Duration.ZERO.toString());
- assertEquals("PT1S", Duration.parse("PT1S").toString());
- assertEquals("PT2M1S", Duration.parse("PT2M1S").toString());
- assertEquals("PT3H2M1S", Duration.parse("PT3H2M1S").toString());
- assertEquals("P4DT3H2M1S", Duration.parse("P4DT3H2M1S").toString());
- }
-
- @Test
- public void testPrefixPNotRequired() {
- assertEquals("PT1S", Duration.parse("T1S").toString());
- assertEquals("PT2M1S", Duration.parse("T2M1S").toString());
- assertEquals("PT3H2M1S", Duration.parse("T3H2M1S").toString());
- assertEquals("P4DT3H2M1S", Duration.parse("4DT3H2M1S").toString());
- }
-
- @Test
- public void testInfixTNotRequired() {
- assertEquals("PT1S", Duration.parse("P1S").toString());
- assertEquals("PT2M1S", Duration.parse("P2M1S").toString());
- assertEquals("PT3H2M1S", Duration.parse("P3H2M1S").toString());
- assertEquals("P4DT3H2M1S", Duration.parse("P4D3H2M1S").toString());
- }
-
- @Test
- public void testPrefixPAndInfixTNotRequired() {
- assertEquals("PT1S", Duration.parse("1S").toString());
- assertEquals("PT2M1S", Duration.parse("2M1S").toString());
- assertEquals("PT3H2M1S", Duration.parse("3H2M1S").toString());
- assertEquals("P4DT3H2M1S", Duration.parse("4D3H2M1S").toString());
- }
-
- @Test
- public void testCompareTo() {
- assertEquals(-1, Duration.parse("PT1S").compareTo(Duration.parse("PT2S")));
- assertEquals(-1, Duration.parse("PT1M").compareTo(Duration.parse("PT2M")));
- assertEquals(-1, Duration.parse("PT1H").compareTo(Duration.parse("PT2H")));
- assertEquals(-1, Duration.parse("P1D").compareTo(Duration.parse("P2D")));
-
- assertEquals(0, Duration.parse("PT1S").compareTo(Duration.parse("PT1S")));
- assertEquals(0, Duration.parse("PT1M").compareTo(Duration.parse("PT1M")));
- assertEquals(0, Duration.parse("PT1H").compareTo(Duration.parse("PT1H")));
- assertEquals(0, Duration.parse("P1D").compareTo(Duration.parse("P1D")));
-
- assertEquals(1, Duration.parse("PT2S").compareTo(Duration.parse("PT1S")));
- assertEquals(1, Duration.parse("PT2M").compareTo(Duration.parse("PT1M")));
- assertEquals(1, Duration.parse("PT2H").compareTo(Duration.parse("PT1H")));
- assertEquals(1, Duration.parse("P2D").compareTo(Duration.parse("P1D")));
-
- assertEquals(0, Duration.parse("PT1M").compareTo(Duration.parse("PT60S")));
- assertEquals(0, Duration.parse("PT1H").compareTo(Duration.parse("PT60M")));
- assertEquals(0, Duration.parse("PT1H").compareTo(Duration.parse("PT3600S")));
- assertEquals(0, Duration.parse("P1D").compareTo(Duration.parse("PT24H")));
- assertEquals(0, Duration.parse("P1D").compareTo(Duration.parse("PT1440M")));
- }
-
- @Test
- public void testEquals() {
- assertNotEquals(Duration.parse("PT1S"), (Duration.parse("PT2S")));
- assertNotEquals(Duration.parse("PT1M"), (Duration.parse("PT2M")));
- assertNotEquals(Duration.parse("PT1H"), (Duration.parse("PT2H")));
- assertNotEquals(Duration.parse("P1D"), (Duration.parse("P2D")));
-
- assertEquals(Duration.parse("PT1S"), (Duration.parse("PT1S")));
- assertEquals(Duration.parse("PT1M"), (Duration.parse("PT1M")));
- assertEquals(Duration.parse("PT1H"), (Duration.parse("PT1H")));
- assertEquals(Duration.parse("P1D"), (Duration.parse("P1D")));
-
- assertEquals(Duration.parse("PT1M"), (Duration.parse("PT60S")));
- assertEquals(Duration.parse("PT1H"), (Duration.parse("PT60M")));
- assertEquals(Duration.parse("PT1H"), (Duration.parse("PT3600S")));
- assertEquals(Duration.parse("P1D"), (Duration.parse("PT24H")));
- assertEquals(Duration.parse("P1D"), (Duration.parse("PT1440M")));
- }
-}
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModifiedTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModifiedTest.java
index c30be8ebff..6b7fff25a3 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModifiedTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModifiedTest.java
@@ -21,7 +21,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.file.attribute.FileTime;
+import java.time.Duration;
import org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes;
+import org.apache.logging.log4j.core.time.Clock;
+import org.apache.logging.log4j.core.time.ClockFactory;
import org.junit.jupiter.api.Test;
/**
@@ -29,17 +32,23 @@ import org.junit.jupiter.api.Test;
*/
public class IfLastModifiedTest {
+ private static final Clock CLOCK = ClockFactory.getClock();
+
@Test
public void testGetDurationReturnsConstructorValue() {
- final IfLastModified filter =
- IfLastModified.newBuilder().setAge(Duration.parse("P7D")).get();
+ final IfLastModified filter = IfLastModified.newBuilder()
+ .setAge(Duration.parse("P7D"))
+ .setClock(CLOCK)
+ .get();
assertEquals(0, filter.getAge().compareTo(Duration.parse("P7D")));
}
@Test
public void testAcceptsIfFileAgeEqualToDuration() {
- final IfLastModified filter =
- IfLastModified.newBuilder().setAge(Duration.parse("PT33S")).get();
+ final IfLastModified filter = IfLastModified.newBuilder()
+ .setAge(Duration.parse("PT33S"))
+ .setClock(CLOCK)
+ .get();
final DummyFileAttributes attrs = new DummyFileAttributes();
final long age = 33 * 1000;
attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age);
@@ -48,8 +57,10 @@ public class IfLastModifiedTest {
@Test
public void testAcceptsIfFileAgeExceedsDuration() {
- final IfLastModified filter =
- IfLastModified.newBuilder().setAge(Duration.parse("PT33S")).get();
+ final IfLastModified filter = IfLastModified.newBuilder()
+ .setAge(Duration.parse("PT33S"))
+ .setClock(CLOCK)
+ .get();
final DummyFileAttributes attrs = new DummyFileAttributes();
final long age = 33 * 1000 + 5;
attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age);
@@ -58,8 +69,10 @@ public class IfLastModifiedTest {
@Test
public void testDoesNotAcceptIfFileAgeLessThanDuration() {
- final IfLastModified filter =
- IfLastModified.newBuilder().setAge(Duration.parse("PT33S")).get();
+ final IfLastModified filter = IfLastModified.newBuilder()
+ .setAge(Duration.parse("PT33S"))
+ .setClock(CLOCK)
+ .get();
final DummyFileAttributes attrs = new DummyFileAttributes();
final long age = 33 * 1000 - 5;
attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age);
@@ -72,6 +85,7 @@ public class IfLastModifiedTest {
final IfLastModified filter = IfLastModified.newBuilder()
.setAge(Duration.parse("PT33S"))
.setNestedConditions(counter)
+ .setClock(CLOCK)
.get();
final DummyFileAttributes attrs = new DummyFileAttributes();
final long oldEnough = 33 * 1000 + 5;
@@ -100,6 +114,7 @@ public class IfLastModifiedTest {
final IfLastModified filter = IfLastModified.newBuilder()
.setAge(Duration.parse("PT33S"))
.setNestedConditions(counter, counter, counter)
+ .setClock(CLOCK)
.get();
filter.beforeFileTreeWalk();
assertEquals(3, counter.getBeforeFileTreeWalkCount());
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
index 52b3cb3b2e..4bffe54cc9 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
@@ -19,23 +19,25 @@ package org.apache.logging.log4j.core.appender.rolling.action;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
+import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.time.Clock;
-import org.apache.logging.log4j.core.time.ClockFactory;
import org.apache.logging.log4j.plugins.Configurable;
import org.apache.logging.log4j.plugins.Inject;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.plugins.PluginAttribute;
import org.apache.logging.log4j.plugins.PluginElement;
import org.apache.logging.log4j.plugins.PluginFactory;
+import org.apache.logging.log4j.plugins.validation.constraints.Required;
import org.apache.logging.log4j.status.StatusLogger;
/**
* PathCondition that accepts paths that are older than the specified duration.
+ * @since 2.5
*/
@Configurable(printObject = true)
@Plugin
@@ -49,9 +51,12 @@ public final class IfLastModified implements PathCondition {
private IfLastModified(final Duration age, final PathCondition[] nestedConditions, final Clock clock) {
this.age = Objects.requireNonNull(age, "age");
this.nestedConditions = PathCondition.copy(nestedConditions);
- this.clock = clock;
+ this.clock = Objects.requireNonNull(clock, "clock");
}
+ /**
+ * @since 3.0.0
+ */
public Duration getAge() {
return age;
}
@@ -60,12 +65,6 @@ public final class IfLastModified implements PathCondition {
return List.of(nestedConditions);
}
- /*
- * (non-Javadoc)
- *
- * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
- * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
- */
@Override
public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
final FileTime fileTime = attrs.lastModifiedTime();
@@ -81,11 +80,6 @@ public final class IfLastModified implements PathCondition {
return result;
}
- /*
- * (non-Javadoc)
- *
- * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
- */
@Override
public void beforeFileTreeWalk() {
IfAll.beforeFileTreeWalk(nestedConditions);
@@ -97,17 +91,24 @@ public final class IfLastModified implements PathCondition {
return "IfLastModified(age=" + age + nested + ")";
}
+ /**
+ * @since 2.24.0
+ */
@PluginFactory
public static Builder newBuilder() {
return new Builder();
}
- public static class Builder implements Supplier<IfLastModified> {
+ /**
+ * @since 2.24.0
+ */
+ public static final class Builder implements Supplier<IfLastModified> {
private Duration age;
private PathCondition[] nestedConditions;
private Clock clock;
- public Builder setAge(@PluginAttribute final Duration age) {
+ public Builder setAge(
+ @PluginAttribute @Required(message = "No age provided for IfLastModified") final Duration age) {
this.age = age;
return this;
}
@@ -125,9 +126,6 @@ public final class IfLastModified implements PathCondition {
@Override
public IfLastModified get() {
- if (clock == null) {
- clock = ClockFactory.getClock();
- }
return new IfLastModified(age, nestedConditions, clock);
}
}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/CoreTypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/CoreTypeConverters.java
index 289a6171cc..7e4bb70cb8 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/CoreTypeConverters.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/CoreTypeConverters.java
@@ -32,12 +32,12 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.Provider;
import java.security.Security;
+import java.time.Duration;
import java.time.ZoneId;
import java.util.Base64;
import java.util.UUID;
import java.util.regex.Pattern;
import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.core.appender.rolling.action.Duration;
import org.apache.logging.log4j.core.util.CronExpression;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.plugins.convert.TypeConverter;
@@ -96,7 +96,7 @@ public final class CoreTypeConverters {
@Override
public byte[] convert(final String value) {
- byte[] bytes;
+ final byte[] bytes;
if (value == null || value.isEmpty()) {
bytes = new byte[0];
} else if (value.startsWith(PREFIX_BASE64)) {
diff --git a/src/changelog/.3.x.x/remove_duration.xml b/src/changelog/.3.x.x/remove_duration.xml
new file mode 100644
index 0000000000..3e0a4fd618
--- /dev/null
+++ b/src/changelog/.3.x.x/remove_duration.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://logging.apache.org/log4j/changelog"
+ xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.3.xsd"
+ type="removed">
+ <description format="asciidoc">Remove deprecated `org.apache.logging.log4j.core.appender.rolling.action.Duration` class.</description>
+</entry>