1616
1717package org .springframework .boot .logging .log4j2 ;
1818
19+ import java .util .Objects ;
20+
1921import org .apache .logging .log4j .core .LogEvent ;
2022import org .apache .logging .log4j .core .appender .rolling .CompositeTriggeringPolicy ;
2123import org .apache .logging .log4j .core .appender .rolling .CronTriggeringPolicy ;
3234import org .apache .logging .log4j .core .util .Builder ;
3335import org .jspecify .annotations .Nullable ;
3436
37+ import org .springframework .boot .convert .ApplicationConversionService ;
38+ import org .springframework .core .convert .ConversionException ;
3539import org .springframework .util .Assert ;
3640
3741/**
4044 * {@code size-and-time}, and {@code cron}.
4145 *
4246 * @author HoJoo Moon
43- * @since 4.0.0
47+ * @author Stephane Nicoll
48+ * @since 4.1.0
4449 */
4550@ Plugin (name = "SpringBootTriggeringPolicy" , category = Node .CATEGORY , elementType = "TriggeringPolicy" ,
4651 deferChildren = true , printObject = true )
@@ -51,12 +56,12 @@ private SpringBootTriggeringPolicy() {
5156
5257 @ Override
5358 public void initialize (RollingFileManager manager ) {
54- throw new UnsupportedOperationException ("This class should not be instantiated" );
59+ throw new UnsupportedOperationException ();
5560 }
5661
5762 @ Override
5863 public boolean isTriggeringEvent (LogEvent logEvent ) {
59- throw new UnsupportedOperationException ("This class should not be instantiated" );
64+ throw new UnsupportedOperationException ();
6065 }
6166
6267 @ PluginBuilderFactory
@@ -73,7 +78,7 @@ public static class SpringBootTriggeringPolicyBuilder implements Builder<Trigger
7378
7479 private static final String DEFAULT_MAX_FILE_SIZE = "10MB" ;
7580
76- private static final int DEFAULT_TIME_INTERVAL = 1 ;
81+ private static final String DEFAULT_TIME_INTERVAL = "1" ;
7782
7883 private static final String DEFAULT_CRON_EXPRESSION = "0 0 0 * * ?" ;
7984
@@ -97,79 +102,51 @@ public static class SpringBootTriggeringPolicyBuilder implements Builder<Trigger
97102
98103 @ Override
99104 public TriggeringPolicy build () {
100- // Read strategy from system properties first, then from attributes
101- String resolvedStrategy = System .getProperty ("LOG4J2_ROLLINGPOLICY_STRATEGY" );
102- if (resolvedStrategy == null ) {
103- resolvedStrategy = (this .strategy != null ) ? this .strategy : DEFAULT_STRATEGY ;
104- }
105+ RollingPolicyStrategy resolvedStrategy = getRollingPolicyStrategy ();
105106 return switch (resolvedStrategy ) {
106- case "time" -> createTimePolicy ();
107- case "size-and-time" -> CompositeTriggeringPolicy .createPolicy (createSizePolicy (), createTimePolicy ());
108- case "cron" -> createCronPolicy ();
109- case "size" -> createSizePolicy ();
110- default -> throw new IllegalArgumentException (
111- "Unsupported rolling policy strategy '%s'" .formatted (resolvedStrategy ));
107+ case TIME -> createTimePolicy ();
108+ case SIZE_AND_TIME -> CompositeTriggeringPolicy .createPolicy (createSizePolicy (), createTimePolicy ());
109+ case CRON -> createCronPolicy ();
110+ case SIZE -> createSizePolicy ();
112111 };
113112 }
114113
115- private TriggeringPolicy createSizePolicy () {
116- // Read from system properties first, then from attributes
117- String size = System . getProperty ( "LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE" );
118- if ( size == null ) {
119- size = ( this . maxFileSize != null ) ? this . maxFileSize : DEFAULT_MAX_FILE_SIZE ;
114+ private static RollingPolicyStrategy getRollingPolicyStrategy () {
115+ String resolvedStrategy = getSystemProperty ( RollingPolicySystemProperty . STRATEGY , DEFAULT_STRATEGY );
116+ try {
117+ return Objects . requireNonNull ( ApplicationConversionService . getSharedInstance ()
118+ . convert ( resolvedStrategy , RollingPolicyStrategy . class )) ;
120119 }
120+ catch (ConversionException ex ) {
121+ throw new IllegalArgumentException (
122+ "Unsupported rolling policy strategy '%s'" .formatted (resolvedStrategy ));
123+ }
124+ }
125+
126+ private TriggeringPolicy createSizePolicy () {
127+ String size = getSystemProperty (RollingPolicySystemProperty .MAX_FILE_SIZE , DEFAULT_MAX_FILE_SIZE );
121128 return SizeBasedTriggeringPolicy .createPolicy (size );
122129 }
123130
124131 private TriggeringPolicy createTimePolicy () {
125- // Read from system properties first, then from attributes
126- String intervalStr = System .getProperty ("LOG4J2_ROLLINGPOLICY_TIME_INTERVAL" );
127- int interval = (intervalStr != null ) ? Integer .parseInt (intervalStr )
128- : (this .timeInterval != null ) ? this .timeInterval : DEFAULT_TIME_INTERVAL ;
129-
130- String modulateStr = System .getProperty ("LOG4J2_ROLLINGPOLICY_TIME_MODULATE" );
131- boolean modulate = (modulateStr != null ) ? Boolean .parseBoolean (modulateStr )
132- : (this .timeModulate != null ) ? this .timeModulate : false ;
133-
132+ int interval = Integer
133+ .parseInt (getSystemProperty (RollingPolicySystemProperty .TIME_INTERVAL , DEFAULT_TIME_INTERVAL ));
134+ boolean modulate = Boolean
135+ .parseBoolean (getSystemProperty (RollingPolicySystemProperty .TIME_MODULATE , Boolean .FALSE .toString ()));
134136 return TimeBasedTriggeringPolicy .newBuilder ().withInterval (interval ).withModulate (modulate ).build ();
135137 }
136138
137139 private TriggeringPolicy createCronPolicy () {
138140 Assert .notNull (this .configuration , "configuration must not be null" );
139141 Configuration configuration = this .configuration ;
140142
141- // Read from system properties first, then from attributes
142- String schedule = System .getProperty ("LOG4J2_ROLLINGPOLICY_CRON_SCHEDULE" );
143- if (schedule == null ) {
144- schedule = (this .cronExpression != null ) ? this .cronExpression : DEFAULT_CRON_EXPRESSION ;
145- }
146-
143+ String schedule = getSystemProperty (RollingPolicySystemProperty .CRON_SCHEDULE , DEFAULT_CRON_EXPRESSION );
147144 return CronTriggeringPolicy .createPolicy (configuration , null , schedule );
148145 }
149146
150- SpringBootTriggeringPolicyBuilder setStrategy (@ Nullable String strategy ) {
151- this .strategy = strategy ;
152- return this ;
153- }
154-
155- SpringBootTriggeringPolicyBuilder setMaxFileSize (@ Nullable String maxFileSize ) {
156- this .maxFileSize = maxFileSize ;
157- return this ;
158- }
159-
160- SpringBootTriggeringPolicyBuilder setTimeInterval (@ Nullable Integer timeInterval ) {
161- this .timeInterval = timeInterval ;
162- return this ;
163- }
164-
165- SpringBootTriggeringPolicyBuilder setTimeModulate (@ Nullable Boolean timeModulate ) {
166- this .timeModulate = timeModulate ;
167- return this ;
168- }
169-
170- SpringBootTriggeringPolicyBuilder setCronExpression (@ Nullable String cronExpression ) {
171- this .cronExpression = cronExpression ;
172- return this ;
147+ private static String getSystemProperty (RollingPolicySystemProperty property , String fallback ) {
148+ String value = System .getProperty (property .getEnvironmentVariableName ());
149+ return (value != null ) ? value : fallback ;
173150 }
174151
175152 SpringBootTriggeringPolicyBuilder setConfiguration (Configuration configuration ) {
0 commit comments