View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.rolling.action;
18  
19  import java.nio.file.Path;
20  import java.nio.file.attribute.BasicFileAttributes;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.logging.log4j.Logger;
26  import org.apache.logging.log4j.core.Core;
27  import org.apache.logging.log4j.core.config.plugins.Plugin;
28  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
29  import org.apache.logging.log4j.core.config.plugins.PluginElement;
30  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
31  import org.apache.logging.log4j.status.StatusLogger;
32  
33  /**
34   * PathCondition that accepts paths after some count threshold is exceeded during the file tree walk.
35   */
36  @Plugin(name = "IfAccumulatedFileCount", category = Core.CATEGORY_NAME, printObject = true)
37  public final class IfAccumulatedFileCount implements PathCondition {
38      private static final Logger LOGGER = StatusLogger.getLogger();
39      private final int threshold;
40      private int count;
41      private final PathCondition[] nestedConditions;
42  
43      private IfAccumulatedFileCount(final int thresholdParam, final PathCondition[] nestedConditions) {
44          if (thresholdParam <= 0) {
45              throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdParam);
46          }
47          this.threshold = thresholdParam;
48          this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
49                  nestedConditions.length);
50      }
51  
52      public int getThresholdCount() {
53          return threshold;
54      }
55  
56      public List<PathCondition> getNestedConditions() {
57          return Collections.unmodifiableList(Arrays.asList(nestedConditions));
58      }
59  
60      /*
61       * (non-Javadoc)
62       *
63       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
64       * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
65       */
66      @Override
67      public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
68          final boolean result = ++count > threshold;
69          final String match = result ? ">" : "<=";
70          final String accept = result ? "ACCEPTED" : "REJECTED";
71          LOGGER.trace("IfAccumulatedFileCount {}: {} count '{}' {} threshold '{}'", accept, relativePath, count, match,
72                  threshold);
73          if (result) {
74              return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
75          }
76          return result;
77      }
78  
79      /*
80       * (non-Javadoc)
81       *
82       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
83       */
84      @Override
85      public void beforeFileTreeWalk() {
86          count = 0;
87          IfAll.beforeFileTreeWalk(nestedConditions);
88      }
89  
90      /**
91       * Create an IfAccumulatedFileCount condition.
92       *
93       * @param threshold The threshold count from which files will be deleted.
94       * @return An IfAccumulatedFileCount condition.
95       */
96      @PluginFactory
97      public static IfAccumulatedFileCount createFileCountCondition(
98              // @formatter:off
99              @PluginAttribute(value = "exceeds", defaultInt = Integer.MAX_VALUE) final int threshold,
100             @PluginElement("PathConditions") final PathCondition... nestedConditions) {
101             // @formatter:on
102 
103         if (threshold == Integer.MAX_VALUE) {
104             LOGGER.error("IfAccumulatedFileCount invalid or missing threshold value.");
105         }
106         return new IfAccumulatedFileCount(threshold, nestedConditions);
107     }
108 
109     @Override
110     public String toString() {
111         final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions);
112         return "IfAccumulatedFileCount(exceeds=" + threshold + nested + ")";
113     }
114 }