1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
62
63
64
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
82
83
84
85 @Override
86 public void beforeFileTreeWalk() {
87 accumulatedSize = 0;
88 IfAll.beforeFileTreeWalk(nestedConditions);
89 }
90
91
92
93
94
95
96
97 @PluginFactory
98 public static IfAccumulatedFileSize createFileSizeCondition(
99
100 @PluginAttribute("exceeds") final String size,
101 @PluginElement("PathConditions") final PathCondition... nestedConditions) {
102
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 }