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.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
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
63
64
65
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
83
84
85
86 @Override
87 public void beforeFileTreeWalk() {
88 accumulatedSize = 0;
89 IfAll.beforeFileTreeWalk(nestedConditions);
90 }
91
92
93
94
95
96
97
98 @PluginFactory
99 public static IfAccumulatedFileSize createFileSizeCondition(
100
101 @PluginAttribute("exceeds") final String size,
102 @PluginElement("PathConditions") final PathCondition... nestedConditions) {
103
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 }