Skip to content

Commit 1725594

Browse files
committed
Rationalize Logback logging properties
Deprecate and provide alternatives for logging properties that are specific to Logback. The following Spring Boot properties have been changed: * logging.pattern.rolling-file-name -> logging.logback.rollingpolicy.file-name-pattern * logging.file.clean-history-on-start -> logging.logback.rollingpolicy.clean-history-on-start * logging.file.max-size -> logging.logback.rollingpolicy.max-file-size * logging.file.total-size-cap -> logging.logback.rollingpolicy.total-size-cap * logging.file.max-history -> logging.logback.rollingpolicy.max-history As have the system environment properties that they map to: * ROLLING_FILE_NAME_PATTERN -> LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN * LOG_FILE_CLEAN_HISTORY_ON_START -> LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START * LOG_FILE_MAX_SIZE -> LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE * LOG_FILE_TOTAL_SIZE_CAP -> LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP * LOG_FILE_MAX_HISTORY -> LOGBACK_ROLLINGPOLICY_MAX_HISTORY This commit also cleans up and simplifies `DefaultLogbackConfiguration`. Closes gh-23609
1 parent 9c54a53 commit 1725594

File tree

15 files changed

+559
-265
lines changed

15 files changed

+559
-265
lines changed

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

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,17 +2040,40 @@ The following table shows how the `logging.*` properties can be used together:
20402040
|===
20412041

20422042
Log files rotate when they reach 10 MB and, as with console output, `ERROR`-level, `WARN`-level, and `INFO`-level messages are logged by default.
2043-
Size limits can be changed using the configprop:logging.file.max-size[] property.
2044-
Rotated log files of the last 7 days are kept by default unless the configprop:logging.file.max-history[] property has been set.
2045-
The total size of log archives can be capped using configprop:logging.file.total-size-cap[].
2046-
When the total size of log archives exceeds that threshold, backups will be deleted.
2047-
To force log archive cleanup on application startup, use the configprop:logging.file.clean-history-on-start[] property.
20482043

20492044
TIP: Logging properties are independent of the actual logging infrastructure.
20502045
As a result, specific configuration keys (such as `logback.configurationFile` for Logback) are not managed by spring Boot.
20512046

20522047

20532048

2049+
[[boot-features-logging-file-rotation]]
2050+
=== File Rotation
2051+
If you are using the Logback, it's possible to fine-tune log rotation settings using your `application.properties` or `application.yaml` file.
2052+
For all other logging system, you'll need to configure rotation settings directly yourself (for example, if you use Log4J2 then you could add a `log4j.xml` file).
2053+
2054+
The following rotation policy properties are supported:
2055+
2056+
|===
2057+
| Name | Description
2058+
2059+
| configprop:logging.logback.rollingpolicy.file-name-pattern[]
2060+
| The filename pattern used to create log archives.
2061+
2062+
| configprop:logging.logback.rollingpolicy.clean-history-on-start[]
2063+
| If log archive cleanup should occur when the application starts.
2064+
2065+
| configprop:logging.logback.rollingpolicy.max-file-size[]
2066+
| The maximum size of log file before it's archived.
2067+
2068+
| configprop:logging.logback.rollingpolicy.total-size-cap[]
2069+
| The maximum amount of size log archives can take before being deleted.
2070+
2071+
| configprop:logging.logback.rollingpolicy.max-history[]
2072+
| The number of days to keep log archives (defaults to 7)
2073+
|===
2074+
2075+
2076+
20542077
[[boot-features-custom-log-levels]]
20552078
=== Log Levels
20562079
All the supported logging systems can have the logger levels set in the Spring `Environment` (for example, in `application.properties`) by using `+logging.level.<logger-name>=<level>+` where `level` is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF.
@@ -2165,64 +2188,62 @@ To help with the customization, some other properties are transferred from the S
21652188
| `LOG_EXCEPTION_CONVERSION_WORD`
21662189
| The conversion word used when logging exceptions.
21672190

2168-
| configprop:logging.file.clean-history-on-start[]
2169-
| `LOG_FILE_CLEAN_HISTORY_ON_START`
2170-
| Whether to clean the archive log files on startup (if LOG_FILE enabled).
2171-
(Only supported with the default Logback setup.)
2172-
21732191
| configprop:logging.file.name[]
21742192
| `LOG_FILE`
21752193
| If defined, it is used in the default log configuration.
21762194

2177-
| configprop:logging.file.max-size[]
2178-
| `LOG_FILE_MAX_SIZE`
2179-
| Maximum log file size (if LOG_FILE enabled).
2180-
(Only supported with the default Logback setup.)
2181-
2182-
| configprop:logging.file.max-history[]
2183-
| `LOG_FILE_MAX_HISTORY`
2184-
| Maximum number of archive log files to keep (if LOG_FILE enabled).
2185-
(Only supported with the default Logback setup.)
2186-
21872195
| configprop:logging.file.path[]
21882196
| `LOG_PATH`
21892197
| If defined, it is used in the default log configuration.
21902198

2191-
| configprop:logging.file.total-size-cap[]
2192-
| `LOG_FILE_TOTAL_SIZE_CAP`
2193-
| Total size of log backups to be kept (if LOG_FILE enabled).
2194-
(Only supported with the default Logback setup.)
2195-
21962199
| configprop:logging.pattern.console[]
21972200
| `CONSOLE_LOG_PATTERN`
21982201
| The log pattern to use on the console (stdout).
2199-
(Only supported with the default Logback setup.)
22002202

22012203
| configprop:logging.pattern.dateformat[]
22022204
| `LOG_DATEFORMAT_PATTERN`
22032205
| Appender pattern for log date format.
2204-
(Only supported with the default Logback setup.)
22052206

22062207
| configprop:logging.pattern.file[]
22072208
| `FILE_LOG_PATTERN`
22082209
| The log pattern to use in a file (if `LOG_FILE` is enabled).
2209-
(Only supported with the default Logback setup.)
22102210

22112211
| configprop:logging.pattern.level[]
22122212
| `LOG_LEVEL_PATTERN`
22132213
| The format to use when rendering the log level (default `%5p`).
2214-
(Only supported with the default Logback setup.)
2215-
2216-
| configprop:logging.pattern.rolling-file-name[]
2217-
| `ROLLING_FILE_NAME_PATTERN`
2218-
| Pattern for rolled-over log file names (default `$\{LOG_FILE}.%d\{yyyy-MM-dd}.%i.gz`).
2219-
(Only supported with the default Logback setup.)
22202214

22212215
| `PID`
22222216
| `PID`
22232217
| The current process ID (discovered if possible and when not already defined as an OS environment variable).
22242218
|===
22252219

2220+
If you're using Logback, the following properties are also transfered:
2221+
2222+
|===
2223+
| Spring Environment | System Property | Comments
2224+
2225+
| configprop:logging.logback.rollingpolicy.file-name-pattern[]
2226+
| `LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN`
2227+
| Pattern for rolled-over log file names (default `$\{LOG_FILE}.%d\{yyyy-MM-dd}.%i.gz`).
2228+
2229+
| configprop:logging.logback.rollingpolicy.clean-history-on-start[]
2230+
| `LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START`
2231+
| Whether to clean the archive log files on startup.
2232+
2233+
| configprop:logging.logback.rollingpolicy.max-file-size[]
2234+
| `LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE`
2235+
| Maximum log file size.
2236+
2237+
| configprop:logging.logback.rollingpolicy.total-size-cap[]
2238+
| `LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP`
2239+
| Total size of log backups to be kept.
2240+
2241+
| configprop:logging.logback.rollingpolicy.max-history[]
2242+
| `LOGBACK_ROLLINGPOLICY_MAX_HISTORY`
2243+
| Maximum number of archive log files to keep.
2244+
|===
2245+
2246+
22262247
All the supported logging systems can consult System properties when parsing their configuration files.
22272248
See the default configurations in `spring-boot.jar` for examples:
22282249

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private void onApplicationFailedEvent() {
271271
* @param classLoader the classloader
272272
*/
273273
protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) {
274-
new LoggingSystemProperties(environment).apply();
274+
getLoggingSystemProperties(environment).apply();
275275
this.logFile = LogFile.get(environment);
276276
if (this.logFile != null) {
277277
this.logFile.applyToSystemProperties();
@@ -283,6 +283,11 @@ protected void initialize(ConfigurableEnvironment environment, ClassLoader class
283283
registerShutdownHookIfNecessary(environment, this.loggingSystem);
284284
}
285285

286+
private LoggingSystemProperties getLoggingSystemProperties(ConfigurableEnvironment environment) {
287+
return (this.loggingSystem != null) ? this.loggingSystem.getSystemProperties(environment)
288+
: new LoggingSystemProperties(environment);
289+
}
290+
286291
private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
287292
if (this.parseArgs && this.springBootLogging == null) {
288293
if (isSet(environment, "debug")) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Set;
2424

25+
import org.springframework.core.env.ConfigurableEnvironment;
2526
import org.springframework.util.Assert;
2627
import org.springframework.util.ClassUtils;
2728
import org.springframework.util.StringUtils;
@@ -57,6 +58,16 @@ public abstract class LoggingSystem {
5758

5859
private static final LoggingSystemFactory SYSTEM_FACTORY = LoggingSystemFactory.fromSpringFactories();
5960

61+
/**
62+
* Return the {@link LoggingSystemProperties} that should be applied.
63+
* @param environment the {@link ConfigurableEnvironment} used to obtain value
64+
* @return the {@link LoggingSystemProperties} to apply
65+
* @since 2.4.0
66+
*/
67+
public LoggingSystemProperties getSystemProperties(ConfigurableEnvironment environment) {
68+
return new LoggingSystemProperties(environment);
69+
}
70+
6071
/**
6172
* Reset the logging system to be limit output. This method may be called before
6273
* {@link #initialize(LoggingInitializationContext, String, LogFile)} to reduce

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

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,30 +62,51 @@ public class LoggingSystemProperties {
6262
public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN";
6363

6464
/**
65-
* The name of the System property that contains the clean history on start flag.
65+
* The name of the System property that contains the file log pattern.
6666
*/
67-
public static final String FILE_CLEAN_HISTORY_ON_START = "LOG_FILE_CLEAN_HISTORY_ON_START";
67+
public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN";
6868

6969
/**
70-
* The name of the System property that contains the file log pattern.
70+
* The name of the System property that contains the rolled-over log file name
71+
* pattern.
72+
* @deprecated since 2.4.0 in favor of
73+
* {@link org.springframework.boot.logging.logback.LogbackLoggingSystemProperties#ROLLINGPOLICY_FILE_NAME_PATTERN}
7174
*/
72-
public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN";
75+
@Deprecated
76+
public static final String ROLLING_FILE_NAME_PATTERN = "ROLLING_FILE_NAME_PATTERN";
7377

7478
/**
75-
* The name of the System property that contains the file log max history.
79+
* The name of the System property that contains the clean history on start flag.
80+
* @deprecated since 2.4.0 in favor of
81+
* {@link org.springframework.boot.logging.logback.LogbackLoggingSystemProperties#ROLLINGPOLICY_CLEAN_HISTORY_ON_START}
7682
*/
77-
public static final String FILE_MAX_HISTORY = "LOG_FILE_MAX_HISTORY";
83+
@Deprecated
84+
public static final String FILE_CLEAN_HISTORY_ON_START = "LOG_FILE_CLEAN_HISTORY_ON_START";
7885

7986
/**
8087
* The name of the System property that contains the file log max size.
88+
* @deprecated since 2.4.0 in favor of
89+
* {@link org.springframework.boot.logging.logback.LogbackLoggingSystemProperties#ROLLINGPOLICY_MAX_FILE_SIZE}
8190
*/
91+
@Deprecated
8292
public static final String FILE_MAX_SIZE = "LOG_FILE_MAX_SIZE";
8393

8494
/**
8595
* The name of the System property that contains the file total size cap.
96+
* @deprecated since 2.4.0 in favor of
97+
* {@link org.springframework.boot.logging.logback.LogbackLoggingSystemProperties#ROLLINGPOLICY_TOTAL_SIZE_CAP}
8698
*/
99+
@Deprecated
87100
public static final String FILE_TOTAL_SIZE_CAP = "LOG_FILE_TOTAL_SIZE_CAP";
88101

102+
/**
103+
* The name of the System property that contains the file log max history.
104+
* @deprecated since 2.4.0 in favor of
105+
* {@link org.springframework.boot.logging.logback.LogbackLoggingSystemProperties#ROLLINGPOLICY_MAX_HISTORY}
106+
*/
107+
@Deprecated
108+
public static final String FILE_MAX_HISTORY = "LOG_FILE_MAX_HISTORY";
109+
89110
/**
90111
* The name of the System property that contains the log level pattern.
91112
*/
@@ -96,12 +117,6 @@ public class LoggingSystemProperties {
96117
*/
97118
public static final String LOG_DATEFORMAT_PATTERN = "LOG_DATEFORMAT_PATTERN";
98119

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

107122
/**
@@ -113,43 +128,52 @@ public LoggingSystemProperties(Environment environment) {
113128
this.environment = environment;
114129
}
115130

116-
public void apply() {
131+
public final void apply() {
117132
apply(null);
118133
}
119134

120-
public void apply(LogFile logFile) {
135+
public final void apply(LogFile logFile) {
121136
PropertyResolver resolver = getPropertyResolver();
122-
setSystemProperty(resolver, EXCEPTION_CONVERSION_WORD, "exception-conversion-word");
137+
apply(logFile, resolver);
138+
}
139+
140+
protected void apply(LogFile logFile, PropertyResolver resolver) {
141+
setSystemProperty(resolver, EXCEPTION_CONVERSION_WORD, "logging.exception-conversion-word");
123142
setSystemProperty(PID_KEY, new ApplicationPid().toString());
124-
setSystemProperty(resolver, CONSOLE_LOG_PATTERN, "pattern.console");
125-
setSystemProperty(resolver, FILE_LOG_PATTERN, "pattern.file");
126-
setSystemProperty(resolver, FILE_CLEAN_HISTORY_ON_START, "file.clean-history-on-start");
127-
setSystemProperty(resolver, FILE_MAX_HISTORY, "file.max-history");
128-
setSystemProperty(resolver, FILE_MAX_SIZE, "file.max-size");
129-
setSystemProperty(resolver, FILE_TOTAL_SIZE_CAP, "file.total-size-cap");
130-
setSystemProperty(resolver, LOG_LEVEL_PATTERN, "pattern.level");
131-
setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "pattern.dateformat");
132-
setSystemProperty(resolver, ROLLING_FILE_NAME_PATTERN, "pattern.rolling-file-name");
143+
setSystemProperty(resolver, CONSOLE_LOG_PATTERN, "logging.pattern.console");
144+
setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "logging.pattern.dateformat");
145+
setSystemProperty(resolver, FILE_LOG_PATTERN, "logging.pattern.file");
146+
setSystemProperty(resolver, LOG_LEVEL_PATTERN, "logging.pattern.level");
147+
applyDeprecated(resolver);
133148
if (logFile != null) {
134149
logFile.applyToSystemProperties();
135150
}
136151
}
137152

153+
private void applyDeprecated(PropertyResolver resolver) {
154+
setSystemProperty(resolver, FILE_CLEAN_HISTORY_ON_START, "logging.file.clean-history-on-start");
155+
setSystemProperty(resolver, FILE_MAX_HISTORY, "logging.file.max-history");
156+
setSystemProperty(resolver, FILE_MAX_SIZE, "logging.file.max-size");
157+
setSystemProperty(resolver, FILE_TOTAL_SIZE_CAP, "logging.file.total-size-cap");
158+
setSystemProperty(resolver, ROLLING_FILE_NAME_PATTERN, "logging.pattern.rolling-file-name");
159+
}
160+
138161
private PropertyResolver getPropertyResolver() {
139162
if (this.environment instanceof ConfigurableEnvironment) {
140163
PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(
141164
((ConfigurableEnvironment) this.environment).getPropertySources());
165+
resolver.setConversionService(((ConfigurableEnvironment) this.environment).getConversionService());
142166
resolver.setIgnoreUnresolvableNestedPlaceholders(true);
143167
return resolver;
144168
}
145169
return this.environment;
146170
}
147171

148-
private void setSystemProperty(PropertyResolver resolver, String systemPropertyName, String propertyName) {
149-
setSystemProperty(systemPropertyName, resolver.getProperty("logging." + propertyName));
172+
protected final void setSystemProperty(PropertyResolver resolver, String systemPropertyName, String propertyName) {
173+
setSystemProperty(systemPropertyName, resolver.getProperty(propertyName));
150174
}
151175

152-
private void setSystemProperty(String name, String value) {
176+
protected final void setSystemProperty(String name, String value) {
153177
if (System.getProperty(name) == null && value != null) {
154178
System.setProperty(name, value);
155179
}

0 commit comments

Comments
 (0)