Skip to content

Commit 61a608d

Browse files
committed
Allow to disable structured logging for Log4j2
Closes GH-45407 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 2923e66 commit 61a608d

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<Appenders>
1111
<Console name="Console" target="SYSTEM_OUT" follow="true">
1212
<Select>
13+
<SystemPropertyArbiter propertyName="CONSOLE_LOG_STRUCTURED_FORMAT" propertyValue="">
14+
<PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
15+
</SystemPropertyArbiter>
1316
<SystemPropertyArbiter propertyName="CONSOLE_LOG_STRUCTURED_FORMAT">
1417
<StructuredLogLayout format="${sys:CONSOLE_LOG_STRUCTURED_FORMAT}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
1518
</SystemPropertyArbiter>
@@ -23,6 +26,9 @@
2326
</Console>
2427
<RollingFile name="File" fileName="${sys:LOG_FILE}" filePattern="${sys:LOG_PATH}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
2528
<Select>
29+
<SystemPropertyArbiter propertyName="FILE_LOG_STRUCTURED_FORMAT" propertyValue="">
30+
<PatternLayout pattern="${sys:FILE_LOG_PATTERN}" charset="${sys:FILE_LOG_CHARSET}"/>
31+
</SystemPropertyArbiter>
2632
<SystemPropertyArbiter propertyName="FILE_LOG_STRUCTURED_FORMAT">
2733
<StructuredLogLayout format="${sys:FILE_LOG_STRUCTURED_FORMAT}" charset="${sys:FILE_LOG_CHARSET}"/>
2834
</SystemPropertyArbiter>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<Appenders>
1111
<Console name="Console" target="SYSTEM_OUT" follow="true">
1212
<Select>
13+
<SystemPropertyArbiter propertyName="CONSOLE_LOG_STRUCTURED_FORMAT" propertyValue="">
14+
<PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
15+
</SystemPropertyArbiter>
1316
<SystemPropertyArbiter propertyName="CONSOLE_LOG_STRUCTURED_FORMAT">
1417
<StructuredLogLayout format="${sys:CONSOLE_LOG_STRUCTURED_FORMAT}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
1518
</SystemPropertyArbiter>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -32,6 +32,7 @@
3232
*
3333
* @author Andy Wilkinson
3434
* @author Scott Frederick
35+
* @author Yanming Zhou
3536
*/
3637
class Log4j2FileXmlTests extends Log4j2XmlTests {
3738

@@ -80,6 +81,12 @@ void whenLogDateformatPatternIsSetThenFileAppenderUsesIt() {
8081
() -> assertThat(fileAppenderPattern()).contains("dd-MM-yyyy"));
8182
}
8283

84+
@Test
85+
void whenFileLogStructuredFormatIsEmptyThenFallbackToPatternLayout() {
86+
withSystemProperty(LoggingSystemProperty.FILE_STRUCTURED_FORMAT.getEnvironmentVariableName(), "",
87+
() -> assertThat(fileAppenderPattern()).contains("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
88+
}
89+
8390
@Override
8491
protected String getConfigFileName() {
8592
return "log4j2-file.xml";

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -36,6 +36,7 @@
3636
*
3737
* @author Andy Wilkinson
3838
* @author Scott Frederick
39+
* @author Yanming Zhou
3940
*/
4041
class Log4j2XmlTests {
4142

@@ -79,6 +80,12 @@ void whenLogDateformatPatternIsSetThenConsoleUsesIt() {
7980
() -> assertThat(consolePattern()).contains("dd-MM-yyyy"));
8081
}
8182

83+
@Test
84+
void whenConsoleStructuredFormatIsEmptyThenFallbackToPatternLayout() {
85+
withSystemProperty(LoggingSystemProperty.CONSOLE_STRUCTURED_FORMAT.getEnvironmentVariableName(), "",
86+
() -> assertThat(consolePattern()).contains("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
87+
}
88+
8289
protected void withSystemProperty(String name, String value, Runnable action) {
8390
String previous = System.setProperty(name, value);
8491
action.run();

0 commit comments

Comments
 (0)