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 14:04:24 UTC

(logging-log4j2) branch deprecate_duration created (now 62262d32b8)

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

pkarwasz pushed a change to branch deprecate_duration
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


      at 62262d32b8 Deprecate `o.a.l.l.c.a.r.a.Duration` class for removal

This branch includes the following new commits:

     new 62262d32b8 Deprecate `o.a.l.l.c.a.r.a.Duration` class for removal

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: Deprecate `o.a.l.l.c.a.r.a.Duration` class for removal

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch deprecate_duration
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 62262d32b80673c884e2eb3d782ce30d66cd2b47
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Thu Mar 28 15:03:58 2024 +0100

    Deprecate `o.a.l.l.c.a.r.a.Duration` class for removal
---
 .../rolling/action/IfLastModifiedTest.java         |  7 +-----
 .../core/appender/rolling/action/Duration.java     |  9 +++++++
 .../appender/rolling/action/IfLastModified.java    | 28 ++++++++++++++++------
 .../config/plugins/convert/TypeConverters.java     | 21 +++++++++++++---
 src/changelog/.2.x.x/deprecate_duration.xml        |  7 ++++++
 5 files changed, 56 insertions(+), 16 deletions(-)

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 333af1ebd4..7160fa14f4 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
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.nio.file.attribute.FileTime;
+import java.time.Duration;
 import java.util.List;
 import org.apache.logging.log4j.core.config.Node;
 import org.apache.logging.log4j.core.config.NullConfiguration;
@@ -43,12 +44,6 @@ import org.junitpioneer.jupiter.SetSystemProperty;
 @SetSystemProperty(key = "log4j2.status.entries", value = "10")
 class IfLastModifiedTest {
 
-    @Test
-    public void testGetDurationReturnsConstructorValue() {
-        final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("P7D"));
-        assertEquals(0, filter.getAge().compareTo(Duration.parse("P7D")));
-    }
-
     @Test
     public void testAcceptsIfFileAgeEqualToDuration() {
         final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S"));
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/Duration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/Duration.java
index 597eceea24..a9c4f9437b 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/Duration.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/Duration.java
@@ -30,7 +30,9 @@ import java.util.regex.Pattern;
  * This implementation does not support fractions or negative values.
  *
  * @see #parse(CharSequence)
+ * @deprecated since 2.24.0 use {@link java.time.Duration} instead.
  */
+@Deprecated
 public class Duration implements Serializable, Comparable<Duration> {
     private static final long serialVersionUID = -3756810052716342061L;
 
@@ -66,6 +68,13 @@ public class Duration implements Serializable, Comparable<Duration> {
     private static final Pattern PATTERN = Pattern.compile(
             "P?(?:([0-9]+)D)?" + "(T?(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)?S)?)?", Pattern.CASE_INSENSITIVE);
 
+    /**
+     * @since 2.24.0
+     */
+    public static Duration ofMillis(final long millis) {
+        return new Duration(millis / 1000L);
+    }
+
     /**
      * The number of seconds in the duration.
      */
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 6b15f5409f..ede472cbdd 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,6 +19,7 @@ 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.Collections;
 import java.util.List;
@@ -50,8 +51,12 @@ public final class IfLastModified implements PathCondition {
         this.nestedConditions = PathCondition.copy(nestedConditions);
     }
 
-    public Duration getAge() {
-        return age;
+    /**
+     * @deprecated since 2.24.0 without a replacement.
+     */
+    @Deprecated
+    public org.apache.logging.log4j.core.appender.rolling.action.Duration getAge() {
+        return org.apache.logging.log4j.core.appender.rolling.action.Duration.ofMillis(age.toMillis());
     }
 
     public List<PathCondition> getNestedConditions() {
@@ -89,20 +94,29 @@ public final class IfLastModified implements PathCondition {
         IfAll.beforeFileTreeWalk(nestedConditions);
     }
 
+    /**
+     * @deprecated since 2.24.0 use {@link #createAgeCondition(Duration, PathCondition...)} instead.
+     */
+    @Deprecated
+    public static IfLastModified createAgeCondition(
+            final org.apache.logging.log4j.core.appender.rolling.action.Duration age,
+            final PathCondition... pathConditions) {
+        return createAgeCondition(Duration.ofMillis(age.toMillis()), pathConditions);
+    }
+
     /**
      * Create an IfLastModified condition.
      *
      * @param age The path age that is accepted by this condition. Must be a valid Duration.
-     * @param nestedConditions nested conditions to evaluate if this condition accepts a path
+     * @param pathConditions Nested conditions to evaluate if this condition accepts a path.
      * @return An IfLastModified condition.
+     * @since 2.24.0
      */
     @PluginFactory
     public static IfLastModified createAgeCondition(
-            // @formatter:off
             @PluginAttribute("age") @Required(message = "No age provided for IfLastModified") final Duration age,
-            @PluginElement("PathConditions") final PathCondition... nestedConditions) {
-        // @formatter:on
-        return new IfLastModified(age, nestedConditions);
+            @PluginElement("pathConditions") final PathCondition... pathConditions) {
+        return new IfLastModified(age, Objects.requireNonNull(pathConditions));
     }
 
     @Override
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
index 987e68e9cc..396dea94a4 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
@@ -32,11 +32,11 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.security.Provider;
 import java.security.Security;
+import java.time.Duration;
 import java.util.UUID;
 import java.util.regex.Pattern;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.appender.rolling.action.Duration;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.util.CronExpression;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -224,11 +224,26 @@ public final class TypeConverters {
     }
 
     /**
-     * Converts a {@link String} into a {@link Duration}.
+     * Converts a {@link String} into a {@link org.apache.logging.log4j.core.appender.rolling.action.Duration}.
      * @since 2.5
+     * @deprecated since 2.24.0 use {@link JavaDurationConverter}
      */
     @Plugin(name = "Duration", category = CATEGORY)
-    public static class DurationConverter implements TypeConverter<Duration> {
+    @Deprecated
+    public static class DurationConverter
+            implements TypeConverter<org.apache.logging.log4j.core.appender.rolling.action.Duration> {
+        @Override
+        public org.apache.logging.log4j.core.appender.rolling.action.Duration convert(final String s) {
+            return org.apache.logging.log4j.core.appender.rolling.action.Duration.parse(s);
+        }
+    }
+
+    /**
+     * Converts a {@link String} into a {@link Duration}.
+     * @since 2.24.0
+     */
+    @Plugin(name = "JavaDuration", category = CATEGORY)
+    public static class JavaDurationConverter implements TypeConverter<Duration> {
         @Override
         public Duration convert(final String s) {
             return Duration.parse(s);
diff --git a/src/changelog/.2.x.x/deprecate_duration.xml b/src/changelog/.2.x.x/deprecate_duration.xml
new file mode 100644
index 0000000000..f11e9c569a
--- /dev/null
+++ b/src/changelog/.2.x.x/deprecate_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="changed">
+  <description format="asciidoc">Deprecate `org.apache.logging.log4j.core.appender.rolling.action.Duration` class for removal.</description>
+</entry>