|
16 | 16 |
|
17 | 17 | package org.springframework.boot.logging.log4j2; |
18 | 18 |
|
19 | | -import java.util.concurrent.TimeUnit; |
20 | | - |
21 | | -import org.apache.logging.log4j.core.LifeCycle; |
22 | | -import org.apache.logging.log4j.core.LifeCycle2; |
23 | 19 | import org.apache.logging.log4j.core.LogEvent; |
24 | | -import org.apache.logging.log4j.core.appender.rolling.AbstractTriggeringPolicy; |
25 | 20 | import org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy; |
26 | 21 | import org.apache.logging.log4j.core.appender.rolling.CronTriggeringPolicy; |
27 | 22 | import org.apache.logging.log4j.core.appender.rolling.RollingFileManager; |
|
34 | 29 | import org.apache.logging.log4j.core.config.plugins.PluginAttribute; |
35 | 30 | import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; |
36 | 31 | import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; |
37 | | -import org.apache.logging.log4j.core.config.plugins.PluginFactory; |
38 | 32 | import org.apache.logging.log4j.core.util.Builder; |
39 | 33 | import org.jspecify.annotations.Nullable; |
40 | 34 |
|
41 | 35 | import org.springframework.util.Assert; |
42 | 36 |
|
43 | 37 | /** |
44 | | - * {@link TriggeringPolicy} that selects one of several standard Log4j2 |
45 | | - * {@link TriggeringPolicy TriggeringPolicies} based on configuration attributes. The |
46 | | - * supported strategies are {@code size}, {@code time}, {@code size-and-time}, and |
47 | | - * {@code cron}. |
| 38 | + * Factory for creating a standard Log4j2 {@link TriggeringPolicy} based on configuration |
| 39 | + * attributes. The supported strategies are {@code size}, {@code time}, |
| 40 | + * {@code size-and-time}, and {@code cron}. |
48 | 41 | * |
49 | 42 | * @author HoJoo Moon |
50 | 43 | * @since 4.0.0 |
51 | 44 | */ |
52 | 45 | @Plugin(name = "SpringBootTriggeringPolicy", category = Node.CATEGORY, elementType = "TriggeringPolicy", |
53 | 46 | deferChildren = true, printObject = true) |
54 | | -public final class SpringBootTriggeringPolicy extends AbstractTriggeringPolicy { |
55 | | - |
56 | | - private final TriggeringPolicy delegate; |
57 | | - |
58 | | - private SpringBootTriggeringPolicy(TriggeringPolicy delegate) { |
59 | | - this.delegate = delegate; |
60 | | - } |
| 47 | +public abstract class SpringBootTriggeringPolicy implements TriggeringPolicy { |
61 | 48 |
|
62 | | - TriggeringPolicy getDelegate() { |
63 | | - return this.delegate; |
| 49 | + private SpringBootTriggeringPolicy() { |
64 | 50 | } |
65 | 51 |
|
66 | 52 | @Override |
67 | 53 | public void initialize(RollingFileManager manager) { |
68 | | - this.delegate.initialize(manager); |
69 | | - } |
70 | | - |
71 | | - @Override |
72 | | - public boolean isTriggeringEvent(LogEvent event) { |
73 | | - return this.delegate.isTriggeringEvent(event); |
74 | | - } |
75 | | - |
76 | | - @Override |
77 | | - public void start() { |
78 | | - super.start(); |
79 | | - if (this.delegate instanceof LifeCycle lifecycle) { |
80 | | - lifecycle.start(); |
81 | | - } |
| 54 | + throw new UnsupportedOperationException("This class should not be instantiated"); |
82 | 55 | } |
83 | 56 |
|
84 | 57 | @Override |
85 | | - public boolean stop(long timeout, TimeUnit timeUnit) { |
86 | | - setStopping(); |
87 | | - boolean result = true; |
88 | | - if (this.delegate instanceof LifeCycle2 lifecycle2) { |
89 | | - result = lifecycle2.stop(timeout, timeUnit); |
90 | | - } |
91 | | - else if (this.delegate instanceof LifeCycle lifecycle) { |
92 | | - lifecycle.stop(); |
93 | | - } |
94 | | - setStopped(); |
95 | | - return result; |
96 | | - } |
97 | | - |
98 | | - @Override |
99 | | - public boolean isStarted() { |
100 | | - if (this.delegate instanceof LifeCycle lifecycle) { |
101 | | - return lifecycle.isStarted(); |
102 | | - } |
103 | | - return super.isStarted(); |
104 | | - } |
105 | | - |
106 | | - @Override |
107 | | - public String toString() { |
108 | | - return "SpringBootTriggeringPolicy{" + this.delegate + "}"; |
| 58 | + public boolean isTriggeringEvent(LogEvent logEvent) { |
| 59 | + throw new UnsupportedOperationException("This class should not be instantiated"); |
109 | 60 | } |
110 | 61 |
|
111 | 62 | @PluginBuilderFactory |
112 | 63 | public static SpringBootTriggeringPolicyBuilder newBuilder() { |
113 | 64 | return new SpringBootTriggeringPolicyBuilder(); |
114 | 65 | } |
115 | 66 |
|
116 | | - @PluginFactory |
117 | | - public static SpringBootTriggeringPolicy createPolicy(@PluginAttribute("strategy") @Nullable String strategy, |
118 | | - @PluginAttribute("maxFileSize") @Nullable String maxFileSize, |
119 | | - @PluginAttribute("timeInterval") @Nullable Integer timeInterval, |
120 | | - @PluginAttribute("timeModulate") @Nullable Boolean timeModulate, |
121 | | - @PluginAttribute("cronExpression") @Nullable String cronExpression, |
122 | | - @PluginConfiguration Configuration configuration) { |
123 | | - return newBuilder().setStrategy(strategy) |
124 | | - .setMaxFileSize(maxFileSize) |
125 | | - .setTimeInterval(timeInterval) |
126 | | - .setTimeModulate(timeModulate) |
127 | | - .setCronExpression(cronExpression) |
128 | | - .setConfiguration(configuration) |
129 | | - .build(); |
130 | | - } |
131 | | - |
132 | 67 | /** |
133 | | - * Builder for {@link SpringBootTriggeringPolicy}. |
| 68 | + * Builder for creating a {@link TriggeringPolicy}. |
134 | 69 | */ |
135 | | - public static class SpringBootTriggeringPolicyBuilder implements Builder<SpringBootTriggeringPolicy> { |
| 70 | + public static class SpringBootTriggeringPolicyBuilder implements Builder<TriggeringPolicy> { |
136 | 71 |
|
137 | 72 | private static final String DEFAULT_STRATEGY = "size"; |
138 | 73 |
|
@@ -161,21 +96,20 @@ public static class SpringBootTriggeringPolicyBuilder implements Builder<SpringB |
161 | 96 | private @Nullable Configuration configuration; |
162 | 97 |
|
163 | 98 | @Override |
164 | | - public SpringBootTriggeringPolicy build() { |
| 99 | + public TriggeringPolicy build() { |
165 | 100 | // Read strategy from system properties first, then from attributes |
166 | 101 | String resolvedStrategy = System.getProperty("LOG4J2_ROLLINGPOLICY_STRATEGY"); |
167 | 102 | if (resolvedStrategy == null) { |
168 | 103 | resolvedStrategy = (this.strategy != null) ? this.strategy : DEFAULT_STRATEGY; |
169 | 104 | } |
170 | | - TriggeringPolicy policy = switch (resolvedStrategy) { |
| 105 | + return switch (resolvedStrategy) { |
171 | 106 | case "time" -> createTimePolicy(); |
172 | 107 | case "size-and-time" -> CompositeTriggeringPolicy.createPolicy(createSizePolicy(), createTimePolicy()); |
173 | 108 | case "cron" -> createCronPolicy(); |
174 | 109 | case "size" -> createSizePolicy(); |
175 | 110 | default -> throw new IllegalArgumentException( |
176 | 111 | "Unsupported rolling policy strategy '%s'".formatted(resolvedStrategy)); |
177 | 112 | }; |
178 | | - return new SpringBootTriggeringPolicy(policy); |
179 | 113 | } |
180 | 114 |
|
181 | 115 | private TriggeringPolicy createSizePolicy() { |
|
0 commit comments