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