Skip to content

Commit 7728f8f

Browse files
Henrikphilwebb
authored andcommitted
Introduce configurable logging.pattern.dateformat
Add configurable logger dateformat support for Logback and Log4j2. Closes gh-8516
1 parent d50fe88 commit 7728f8f

File tree

9 files changed

+46
-13
lines changed

9 files changed

+46
-13
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ content into your application; rather pick only the properties that you need.
4848
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
4949
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
5050
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
51+
logging.pattern.dateformat= # Appender pattern for log dateformat (default yyyy-MM-dd HH:mm:ss.SSS). Only supported with the default logback setup.
5152
logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.
5253
5354
# AOP

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public class LoggingSystemProperties {
7979
*/
8080
public static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN";
8181

82+
/**
83+
* The name of the System property that contains the log date-format pattern.
84+
*/
85+
public static final String LOG_DATEFORMAT_PATTERN = "LOG_DATEFORMAT_PATTERN";
86+
8287
private final Environment environment;
8388

8489
/**
@@ -103,6 +108,7 @@ public void apply(LogFile logFile) {
103108
setSystemProperty(resolver, FILE_MAX_HISTORY, "file.max-history");
104109
setSystemProperty(resolver, FILE_MAX_SIZE, "file.max-size");
105110
setSystemProperty(resolver, LOG_LEVEL_PATTERN, "pattern.level");
111+
setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "pattern.dateformat");
106112
setSystemProperty(PID_KEY, new ApplicationPid().toString());
107113
if (logFile != null) {
108114
logFile.applyToSystemProperties();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
*/
5151
class DefaultLogbackConfiguration {
5252

53-
private static final String CONSOLE_LOG_PATTERN = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} "
53+
private static final String CONSOLE_LOG_PATTERN = "%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} "
5454
+ "%clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} "
5555
+ "%clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} "
5656
+ "%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
5757

58-
private static final String FILE_LOG_PATTERN = "%d{yyyy-MM-dd HH:mm:ss.SSS} "
58+
private static final String FILE_LOG_PATTERN = "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} "
5959
+ "${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
6060

6161
private static final String MAX_FILE_SIZE = "10MB";

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.boot.logging.LoggingInitializationContext;
4444
import org.springframework.boot.logging.LoggingSystem;
4545
import org.springframework.boot.logging.Slf4JLoggingSystem;
46+
import org.springframework.core.env.Environment;
4647
import org.springframework.util.Assert;
4748
import org.springframework.util.ResourceUtils;
4849
import org.springframework.util.StringUtils;
@@ -125,9 +126,11 @@ protected void loadDefaults(LoggingInitializationContext initializationContext,
125126
LoggerContext context = getLoggerContext();
126127
stopAndReset(context);
127128
LogbackConfigurator configurator = new LogbackConfigurator(context);
128-
context.putProperty("LOG_LEVEL_PATTERN",
129-
initializationContext.getEnvironment().resolvePlaceholders(
130-
"${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}"));
129+
Environment environment = initializationContext.getEnvironment();
130+
context.putProperty("LOG_LEVEL_PATTERN", environment.resolvePlaceholders(
131+
"${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}"));
132+
context.putProperty("LOG_DATEFORMAT_PATTERN", environment.resolvePlaceholders(
133+
"${logging.pattern.dateformat:${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}"));
131134
new DefaultLogbackConfiguration(initializationContext, logFile)
132135
.apply(configurator);
133136
context.setPackagingDataEnabled(true);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,27 @@
9494
"type": "java.lang.String",
9595
"description": "Appender pattern for output to the console. Only supported with the default logback setup.",
9696
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
97-
"defaultValue": "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
97+
"defaultValue": "%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
9898
},
9999
{
100100
"name": "logging.pattern.file",
101101
"type": "java.lang.String",
102102
"description": "Appender pattern for output to the file. Only supported with the default logback setup.",
103103
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
104-
"defaultValue": "%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
104+
"defaultValue": "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
105105
},
106106
{
107107
"name": "logging.pattern.level",
108108
"type": "java.lang.String",
109109
"description": "Appender pattern for log level (default %5p). Only supported with the default logback setup.",
110110
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener"
111111
},
112+
{
113+
"name": "logging.pattern.dateformat",
114+
"type": "java.lang.String",
115+
"description": "Appender pattern for log dateformat (default yyyy-MM-dd HH:mm:ss.SSS). Only supported with the default logback setup.",
116+
"sourceType": "org.springframework.boot.logging.LoggingApplicationListener"
117+
},
112118
{
113119
"name": "logging.path",
114120
"type": "java.lang.String",

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
<Property name="PID">????</Property>
55
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
66
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
7-
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
8-
<Property name="FILE_LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN} ${sys:PID} --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
7+
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
8+
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
9+
<Property name="FILE_LOG_PATTERN">%d{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} ${sys:PID} --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
10+
911
</Properties>
1012
<Appenders>
1113
<Console name="Console" target="SYSTEM_OUT" follow="true">

spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
<Property name="PID">????</Property>
55
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
66
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
7-
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
8-
<Property name="FILE_LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN} ${sys:PID} --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
7+
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
8+
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
9+
<Property name="FILE_LOG_PATTERN">%d{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} ${sys:PID} --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
910
</Properties>
1011
<Appenders>
1112
<Console name="Console" target="SYSTEM_OUT" follow="true">

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ initialization performed by Boot
99
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
1010
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
1111
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
12-
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
13-
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
12+
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
13+
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
1414

1515
<appender name="DEBUG_LEVEL_REMAPPER" class="org.springframework.boot.logging.logback.LevelRemappingAppender">
1616
<destinationLogger>org.springframework.boot</destinationLogger>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,20 @@ public void initializationIsOnlyPerformedOnceUntilCleanedUp() throws Exception {
440440
verify(listener, times(2)).onReset(loggerContext);
441441
}
442442

443+
@Test
444+
public void testDateformatPatternProperty() {
445+
MockEnvironment environment = new MockEnvironment();
446+
environment.setProperty("logging.pattern.dateformat",
447+
"yyyy-MM-dd'T'hh:mm:ss.SSSZ");
448+
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
449+
environment);
450+
this.loggingSystem.initialize(loggingInitializationContext, null, null);
451+
this.logger.info("Hello world");
452+
String output = this.output.toString().trim();
453+
assertThat(getLineWithText(output, "Hello world"))
454+
.containsPattern("\\d{4}-\\d{2}\\-\\d{2}T\\d{2}:\\d{2}:\\d{2}");
455+
}
456+
443457
private static Logger getRootLogger() {
444458
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
445459
LoggerContext context = (LoggerContext) factory;

0 commit comments

Comments
 (0)