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.appender.rolling.FileSize;
28  import org.apache.logging.log4j.core.config.plugins.Plugin;
29  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
30  import org.apache.logging.log4j.core.config.plugins.PluginElement;
31  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
32  import org.apache.logging.log4j.status.StatusLogger;
33  
34  /**
35   * PathCondition that accepts paths after the accumulated file size threshold is exceeded during the file tree walk.
36   */
37  @Plugin(name = "IfAccumulatedFileSize", category = Core.CATEGORY_NAME, printObject = true)
38  public final class IfAccumulatedFileSize implements PathCondition {
39      private static final Logger LOGGER = StatusLogger.getLogger();
40      private final long thresholdBytes;
41      private long accumulatedSize;
42      private final PathCondition[] nestedConditions;
43  
44      private IfAccumulatedFileSize(final long thresholdSize, final PathCondition[] nestedConditions) {
45          if (thresholdSize <= 0) {
46              throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdSize);
47          }
48          this.thresholdBytes = thresholdSize;
49          this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
50                  nestedConditions.length);
51      }
52  
53      public long getThresholdBytes() {
54          return thresholdBytes;
55      }
56  
57      public List<PathCondition> getNestedConditions() {
58          return Collections.unmodifiableList(Arrays.asList(nestedConditions));
59      }
60  
61      /*
62       * (non-Javadoc)
63       *
64       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
65       * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
66       */
67      @Override
68      public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
69          accumulatedSize += attrs.size();
70          final boolean result = accumulatedSize > thresholdBytes;
71          final String match = result ? ">" : "<=";
72          final String accept = result ? "ACCEPTED" : "REJECTED";
73          LOGGER.trace("IfAccumulatedFileSize {}: {} accumulated size '{}' {} thresholdBytes '{}'", accept, relativePath,
74                  accumulatedSize, match, thresholdBytes);
75          if (result) {
76              return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
77          }
78          return result;
79      }
80  
81      /*
82       * (non-Javadoc)
83       *
84       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
85       */
86      @Override
87      public void beforeFileTreeWalk() {
88          accumulatedSize = 0;
89          IfAll.beforeFileTreeWalk(nestedConditions);
90      }
91  
92      /**
93       * Create an IfAccumulatedFileSize condition.
94       *
95       * @param threshold The threshold accumulated file size from which files will be deleted.
96       * @return An IfAccumulatedFileSize condition.
97       */
98      @PluginFactory
99      public static IfAccumulatedFileSize createFileSizeCondition(
100             // @formatter:off
101             @PluginAttribute("exceeds") final String size,
102             @PluginElement("PathConditions") final PathCondition... nestedConditions) {
103             // @formatter:on
104 
105         if (size == null) {
106             LOGGER.error("IfAccumulatedFileSize missing mandatory size threshold.");
107         }
108         final long threshold = size == null ? Long.MAX_VALUE : FileSize.parse(size, Long.MAX_VALUE);
109         return new IfAccumulatedFileSize(threshold, nestedConditions);
110     }
111 
112     @Override
113     public String toString() {
114         final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions);
115         return "IfAccumulatedFileSize(exceeds=" + thresholdBytes + nested + ")";
116     }
117 }