Skip to content

Commit 4a3810a

Browse files
eddumelendezsnicoll
authored andcommitted
Add ROLLING_FILE_NAME_PATTERN for File Appender
This new property allows to customize `fileNamePattern` if it is set. Otherwise, a default pattern remains. Also, new property is supported `logging.pattern.rolling-file-name`. See gh-18151
1 parent 06f46ba commit 4a3810a

File tree

13 files changed

+94
-5
lines changed

13 files changed

+94
-5
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ The following files are provided under `org/springframework/boot/logging/logback
13071307

13081308
* `defaults.xml` - Provides conversion rules, pattern properties and common logger configurations.
13091309
* `console-appender.xml` - Adds a `ConsoleAppender` using the `CONSOLE_LOG_PATTERN`.
1310-
* `file-appender.xml` - Adds a `RollingFileAppender` using the `FILE_LOG_PATTERN` with appropriate settings.
1310+
* `file-appender.xml` - Adds a `RollingFileAppender` using the `FILE_LOG_PATTERN` and `ROLLING_FILE_NAME_PATTERN` with appropriate settings.
13111311

13121312
In addition, a legacy `base.xml` file is provided for compatibility with earlier versions of Spring Boot.
13131313

@@ -1332,6 +1332,7 @@ Your logback configuration file can also make use of System properties that the
13321332
* `$\{LOG_FILE}`: Whether `logging.file.name` was set in Boot's external configuration.
13331333
* `$\{LOG_PATH}`: Whether `logging.file.path` (representing a directory for log files to live in) was set in Boot's external configuration.
13341334
* `$\{LOG_EXCEPTION_CONVERSION_WORD}`: Whether `logging.exception-conversion-word` was set in Boot's external configuration.
1335+
* `$\{ROLLING_FILE_NAME_PATTERN}`: Whether `logging.pattern.rolling-file-name` was set in Boot's external configuration.
13351336

13361337
Spring Boot also provides some nice ANSI color terminal output on a console (but not in a log file) by using a custom Logback converter.
13371338
See the `CONSOLE_LOG_PATTERN` in the `default.xml` configuration for an example.

spring-boot-project/spring-boot-docs/src/main/asciidoc/index.htmladoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[spring-boot-reference-documentation]]
22
= Spring Boot Reference Documentation
3-
Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, Andy Wilkinson, Marcel Overdijk, Christian Dupuis, Sébastien Deleuze, Michael Simons, Vedran Pavić, Jay Bryant, Madhura Bhave
3+
Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, Andy Wilkinson, Marcel Overdijk, Christian Dupuis, Sébastien Deleuze, Michael Simons, Vedran Pavić, Jay Bryant, Madhura Bhave, Eddú Meléndez
44
:docinfo: shared
55

66
The reference documentation consists of the following sections:

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,11 @@ To help with the customization, some other properties are transferred from the S
17751775
| The format to use when rendering the log level (default `%5p`).
17761776
(Only supported with the default Logback setup.)
17771777

1778+
| `logging.pattern.rolling-file-name`
1779+
| `ROLLING_FILE_NAME_PATTERN`
1780+
| Pattern to use when roll-over log files (default `$\{LOG_FILE}.%d\{yyyy-MM-dd}.%i.gz`).
1781+
(Only supported with the default Logback setup.)
1782+
17781783
| `PID`
17791784
| `PID`
17801785
| The current process ID (discovered if possible and when not already defined as an OS environment variable).

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Madhura Bhave
3232
* @author Vedran Pavic
3333
* @author Robert Thornton
34+
* @author Eddú Meléndez
3435
* @since 2.0.0
3536
*/
3637
public class LoggingSystemProperties {
@@ -95,6 +96,11 @@ public class LoggingSystemProperties {
9596
*/
9697
public static final String LOG_DATEFORMAT_PATTERN = "LOG_DATEFORMAT_PATTERN";
9798

99+
/**
100+
* The name of the System property that contains the rolled-over log file pattern.
101+
*/
102+
public static final String ROLLING_FILE_NAME_PATTERN = "ROLLING_FILE_NAME_PATTERN";
103+
98104
private final Environment environment;
99105

100106
/**
@@ -122,6 +128,7 @@ public void apply(LogFile logFile) {
122128
setSystemProperty(resolver, FILE_TOTAL_SIZE_CAP, "file.total-size-cap");
123129
setSystemProperty(resolver, LOG_LEVEL_PATTERN, "pattern.level");
124130
setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "pattern.dateformat");
131+
setSystemProperty(resolver, ROLLING_FILE_NAME_PATTERN, "pattern.rolling-file-name");
125132
if (logFile != null) {
126133
logFile.applyToSystemProperties();
127134
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* @author Madhura Bhave
4848
* @author Vedran Pavic
4949
* @author Robert Thornton
50+
* @author Eddú Meléndez
5051
*/
5152
class DefaultLogbackConfiguration {
5253

@@ -140,7 +141,8 @@ private void setRollingPolicy(RollingFileAppender<ILoggingEvent> appender, Logba
140141
SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new SizeAndTimeBasedRollingPolicy<>();
141142
rollingPolicy.setCleanHistoryOnStart(
142143
this.patterns.getProperty("logging.file.clean-history-on-start", Boolean.class, false));
143-
rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz");
144+
rollingPolicy.setFileNamePattern(
145+
this.patterns.getProperty("logging.pattern.rolling-file-name", logFile + ".%d{yyyy-MM-dd}.%i.gz"));
144146
setMaxFileSize(rollingPolicy, getDataSize("logging.file.max-size", MAX_FILE_SIZE));
145147
rollingPolicy
146148
.setMaxHistory(this.patterns.getProperty("logging.file.max-history", Integer.class, MAX_FILE_HISTORY));

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @author Dave Syer
6262
* @author Andy Wilkinson
6363
* @author Ben Hale
64+
* @author Eddú Meléndez
6465
* @since 1.0.0
6566
*/
6667
public class LogbackLoggingSystem extends Slf4JLoggingSystem {
@@ -139,6 +140,8 @@ protected void loadDefaults(LoggingInitializationContext initializationContext,
139140
environment.resolvePlaceholders("${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}"));
140141
context.putProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, environment.resolvePlaceholders(
141142
"${logging.pattern.dateformat:${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}"));
143+
context.putProperty(LoggingSystemProperties.ROLLING_FILE_NAME_PATTERN, environment
144+
.resolvePlaceholders("${logging.pattern.rolling-file-name:${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz}"));
142145
new DefaultLogbackConfiguration(initializationContext, logFile).apply(configurator);
143146
context.setPackagingDataEnabled(true);
144147
}

spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@
165165
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
166166
"defaultValue": "%5p"
167167
},
168+
{
169+
"name": "logging.pattern.rolling-file-name",
170+
"type": "java.lang.String",
171+
"description": "Pattern for roll-over log files. Supported only with the default Logback setup.",
172+
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
173+
"defaultValue": "${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz"
174+
},
168175
{
169176
"name": "logging.register-shutdown-hook",
170177
"type": "java.lang.Boolean",

spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ initialization performed by Boot
1414
<file>${LOG_FILE}</file>
1515
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
1616
<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
17-
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
17+
<fileNamePattern>${ROLLING_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz}</fileNamePattern>
1818
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
1919
<maxHistory>${LOG_FILE_MAX_HISTORY:-7}</maxHistory>
2020
<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
* @author Stephane Nicoll
8484
* @author Ben Hale
8585
* @author Fahim Farook
86+
* @author Eddú Meléndez
8687
*/
8788
@ExtendWith(OutputCaptureExtension.class)
8889
@ClassPathExclusions("log4j*.jar")
@@ -134,6 +135,7 @@ void clear() throws IOException {
134135
System.clearProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN);
135136
System.clearProperty(LoggingSystemProperties.FILE_LOG_PATTERN);
136137
System.clearProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN);
138+
System.clearProperty(LoggingSystemProperties.ROLLING_FILE_NAME_PATTERN);
137139
System.clearProperty(LoggingSystem.SYSTEM_PROPERTY);
138140
if (this.context != null) {
139141
this.context.close();
@@ -479,14 +481,17 @@ void closingChildContextDoesNotCleanUpLoggingSystem() {
479481
void systemPropertiesAreSetForLoggingConfiguration() {
480482
addPropertiesToEnvironment(this.context, "logging.exception-conversion-word=conversion",
481483
"logging.file.name=" + this.logFile, "logging.file.path=path", "logging.pattern.console=console",
482-
"logging.pattern.file=file", "logging.pattern.level=level");
484+
"logging.pattern.file=file", "logging.pattern.level=level",
485+
"logging.pattern.rolling-file-name=my.log.%d{yyyyMMdd}.%i.gz");
483486
this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader());
484487
assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN)).isEqualTo("console");
485488
assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_PATTERN)).isEqualTo("file");
486489
assertThat(System.getProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD)).isEqualTo("conversion");
487490
assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)).isEqualTo(this.logFile.getAbsolutePath());
488491
assertThat(System.getProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN)).isEqualTo("level");
489492
assertThat(System.getProperty(LoggingSystemProperties.LOG_PATH)).isEqualTo("path");
493+
assertThat(System.getProperty(LoggingSystemProperties.ROLLING_FILE_NAME_PATTERN))
494+
.isEqualTo("my.log.%d{yyyyMMdd}.%i.gz");
490495
assertThat(System.getProperty(LoggingSystemProperties.PID_KEY)).isNotNull();
491496
}
492497

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* Tests for {@link LoggingSystemProperties}.
3636
*
3737
* @author Andy Wilkinson
38+
* @author Eddú Meléndez
3839
*/
3940
class LoggingSystemPropertiesTests {
4041

@@ -82,6 +83,15 @@ void fileLogPatternCanReferencePid() {
8283
assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_PATTERN)).matches("[0-9]+");
8384
}
8485

86+
@Test
87+
void rollingFileIsSet() {
88+
new LoggingSystemProperties(
89+
new MockEnvironment().withProperty("logging.pattern.rolling-file-name", "rolling file pattern"))
90+
.apply(null);
91+
assertThat(System.getProperty(LoggingSystemProperties.ROLLING_FILE_NAME_PATTERN))
92+
.isEqualTo("rolling file pattern");
93+
}
94+
8595
private Environment environment(String key, Object value) {
8696
StandardEnvironment environment = new StandardEnvironment();
8797
environment.getPropertySources().addLast(new MapPropertySource("test", Collections.singletonMap(key, value)));

0 commit comments

Comments
 (0)