LOG4J2-435 initial version of IfAccumulatedFileCount and
IfAccumulatedFileSize

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4c8d8a1f
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4c8d8a1f
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4c8d8a1f

Branch: refs/heads/master
Commit: 4c8d8a1f4cef01109aa1d48fada9b63b87bc634d
Parents: 34e3485
Author: rpopma <[email protected]>
Authored: Thu Nov 26 23:53:49 2015 +0900
Committer: rpopma <[email protected]>
Committed: Thu Nov 26 23:53:49 2015 +0900

----------------------------------------------------------------------
 .../rolling/action/IfAccumulatedFileCount.java  | 91 +++++++++++++++++++
 .../rolling/action/IfAccumulatedFileSize.java   | 95 ++++++++++++++++++++
 2 files changed, 186 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4c8d8a1f/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
new file mode 100644
index 0000000..8bc8a85
--- /dev/null
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
@@ -0,0 +1,91 @@
+/*
+ * 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 java.nio.file.Path;
+import java.nio.file.attribute.BasicFileAttributes;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * PathCondition that accepts paths after some count threshold is exceeded 
during the file tree walk.
+ */
+@Plugin(name = "IfAccumulatedFileCount", category = "Core", printObject = true)
+public final class IfAccumulatedFileCount implements PathCondition {
+    private static final Logger LOGGER = StatusLogger.getLogger();
+    private final int threshold;
+    private int count;
+
+    private IfAccumulatedFileCount(final int threshold) {
+        if (threshold <= 0) {
+            throw new IllegalArgumentException("Count must be a positive 
integer but was " + threshold);
+        }
+        this.threshold = threshold;
+    }
+
+    public int getThresholdCount() {
+        return threshold;
+    }
+
+    /*
+     * (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 baseDir, final Path relativePath, final 
BasicFileAttributes attrs) {
+        final boolean result = ++count > threshold;
+        final String match = result ? ">" : "<=";
+        LOGGER.trace("IfAccumulatedFileCount: {} count '{}' {} threshold 
'{}'", relativePath, count, match, threshold);
+        return result;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see 
org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
+     */
+    @Override
+    public void beforeFileTreeWalk() {
+        count = 0;
+    }
+
+    /**
+     * Create an IfAccumulatedFileCount condition.
+     * 
+     * @param threshold The threshold count from which files will be deleted.
+     * @return An IfAccumulatedFileCount condition.
+     */
+    @PluginFactory
+    public static IfAccumulatedFileCount createAgeCondition( //
+            @PluginAttribute(value = "exceeds", defaultInt = 
Integer.MAX_VALUE) final int threshold) {
+        if (threshold == Integer.MAX_VALUE) {
+            LOGGER.error("IfAccumulatedFileCount invalid or missing threshold 
value.");
+        }
+        return new IfAccumulatedFileCount(threshold);
+    }
+
+    @Override
+    public String toString() {
+        return "IfAccumulatedFileCount(exceeds=" + threshold + ")";
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4c8d8a1f/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
new file mode 100644
index 0000000..1820ec9
--- /dev/null
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
@@ -0,0 +1,95 @@
+/*
+ * 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 java.nio.file.Path;
+import java.nio.file.attribute.BasicFileAttributes;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.appender.rolling.FileSize;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * PathCondition that accepts paths after the accumulated file size threshold 
is exceeded during the file tree walk.
+ */
+@Plugin(name = "IfAccumulatedFileSize", category = "Core", printObject = true)
+public final class IfAccumulatedFileSize implements PathCondition {
+    private static final Logger LOGGER = StatusLogger.getLogger();
+    private final long thresholdBytes;
+    private long accumulatedSize;
+
+    private IfAccumulatedFileSize(final long thresholdSize) {
+        if (thresholdSize <= 0) {
+            throw new IllegalArgumentException("Count must be a positive 
integer but was " + thresholdSize);
+        }
+        this.thresholdBytes = thresholdSize;
+    }
+
+    public long getThresholdBytes() {
+        return thresholdBytes;
+    }
+
+    /*
+     * (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 baseDir, final Path relativePath, final 
BasicFileAttributes attrs) {
+        accumulatedSize += attrs.size();
+        final boolean result = accumulatedSize > thresholdBytes;
+        final String match = result ? ">" : "<=";
+        LOGGER.trace("IfAccumulatedFileSize: {} accumulated size '{}' {} 
thresholdBytes '{}'", relativePath,
+                accumulatedSize, match, thresholdBytes);
+        return result;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see 
org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
+     */
+    @Override
+    public void beforeFileTreeWalk() {
+        accumulatedSize = 0;
+    }
+
+    /**
+     * Create an IfAccumulatedFileSize condition.
+     * 
+     * @param threshold The threshold accumulated file size from which files 
will be deleted.
+     * @return An IfAccumulatedFileSize condition.
+     */
+    @PluginFactory
+    public static IfAccumulatedFileSize createAgeCondition( //
+            @PluginAttribute("exceeds") final String size) {
+        if (size == null) {
+            LOGGER.error("IfAccumulatedFileSize missing mandatory size 
threshold.");
+        }
+        final long threshold = size == null ? Long.MAX_VALUE : 
FileSize.parse(size, Long.MAX_VALUE);
+        return new IfAccumulatedFileSize(threshold);
+    }
+
+    @Override
+    public String toString() {
+        return "IfAccumulatedFileSize(exceeds=" + thresholdBytes + ")";
+    }
+}

Reply via email to