Skip to content

Commit f6492cd

Browse files
committed
Consume level and dateformat patterns as system props in Log4j config
Previously LOG_LEVEL_PATTERN and LOG_DATEFORMAT_PATTERN were not consumed as system properties in log4j2.xml and log4j2-file.xml. As a result, the logging.pattern.level and logging.pattern.dateformat configuration properties, which are translated into the LOG_LEVEL_PATTERN and LOG_DATEFORMAT_PATTERN system properties respectively had no effect. This commit updates the log4j2.xml and log4j2-file.xml config files to consume LOG_LEVEL_PATTERN and LOG_DATEFORMAT_PATTERN as system properties. When the system property is not set, the configuation falls back to the default values specified in the config files. Tests for both log4j2.xml and log4j2-file.xml to verify the behaviour have also bean added. Fixes gh-22983
1 parent 581190d commit f6492cd

File tree

4 files changed

+205
-3
lines changed

4 files changed

+205
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
55
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
66
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
7-
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{%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{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} %pid --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
7+
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%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{${sys:LOG_DATEFORMAT_PATTERN}} ${sys:LOG_LEVEL_PATTERN} %pid --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
99

1010
</Properties>
1111
<Appenders>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
55
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
66
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
7-
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
7+
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
88
<Property name="FILE_LOG_PATTERN">%d{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} %pid --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
99
</Properties>
1010
<Appenders>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging.log4j2;
18+
19+
import java.io.File;
20+
21+
import org.apache.logging.log4j.core.config.Configuration;
22+
import org.apache.logging.log4j.core.layout.PatternLayout;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.io.TempDir;
27+
28+
import org.springframework.boot.logging.LoggingSystemProperties;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@code log4j2-file.xml}.
34+
*
35+
* @author Andy Wilkinson
36+
*/
37+
class Log4j2FileXmlTests extends Log4j2XmlTests {
38+
39+
@BeforeEach
40+
void configureLogFile(@TempDir File temp) {
41+
System.setProperty(LoggingSystemProperties.LOG_FILE, new File(temp, "test.log").getAbsolutePath());
42+
}
43+
44+
@AfterEach
45+
void clearLogFile() {
46+
System.clearProperty(LoggingSystemProperties.LOG_FILE);
47+
}
48+
49+
@Test
50+
void whenLogExceptionConversionWordIsNotConfiguredThenFileAppenderUsesDefault() {
51+
assertThat(fileAppenderPattern()).contains("%xwEx");
52+
}
53+
54+
@Test
55+
void whenLogExceptionConversionWordIsSetThenFileAppenderUsesIt() {
56+
withSystemProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "custom",
57+
() -> assertThat(fileAppenderPattern()).contains("custom"));
58+
}
59+
60+
@Test
61+
void whenLogLevelPatternIsNotConfiguredThenFileAppenderUsesDefault() {
62+
assertThat(fileAppenderPattern()).contains("%5p");
63+
}
64+
65+
@Test
66+
void whenLogLevelPatternIsSetThenFileAppenderUsesIt() {
67+
withSystemProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN, "custom",
68+
() -> assertThat(fileAppenderPattern()).contains("custom"));
69+
}
70+
71+
@Test
72+
void whenLogLDateformatPatternIsNotConfiguredThenFileAppenderUsesDefault() {
73+
assertThat(fileAppenderPattern()).contains("yyyy-MM-dd HH:mm:ss.SSS");
74+
}
75+
76+
@Test
77+
void whenLogDateformatPatternIsSetThenFileAppenderUsesIt() {
78+
withSystemProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, "custom",
79+
() -> assertThat(fileAppenderPattern()).contains("custom"));
80+
}
81+
82+
@Override
83+
protected String getConfigFileName() {
84+
return "log4j2-file.xml";
85+
}
86+
87+
private String fileAppenderPattern() {
88+
Configuration configuration = initializeConfiguration();
89+
return ((PatternLayout) configuration.getAppender("File").getLayout()).getConversionPattern();
90+
}
91+
92+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging.log4j2;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
import org.apache.logging.log4j.core.LoggerContext;
23+
import org.apache.logging.log4j.core.config.Configuration;
24+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
25+
import org.apache.logging.log4j.core.config.ConfigurationSource;
26+
import org.apache.logging.log4j.core.layout.PatternLayout;
27+
import org.junit.jupiter.api.Test;
28+
29+
import org.springframework.boot.logging.LoggingSystemProperties;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@code log4j2.xml}.
35+
*
36+
* @author Andy Wilkinson
37+
*/
38+
class Log4j2XmlTests {
39+
40+
@Test
41+
void whenLogExceptionConversionWordIsNotConfiguredThenConsoleUsesDefault() {
42+
assertThat(consolePattern()).contains("%xwEx");
43+
}
44+
45+
@Test
46+
void whenLogExceptionConversionWordIsSetThenConsoleUsesIt() {
47+
withSystemProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "custom",
48+
() -> assertThat(consolePattern()).contains("custom"));
49+
}
50+
51+
@Test
52+
void whenLogLevelPatternIsNotConfiguredThenConsoleUsesDefault() {
53+
assertThat(consolePattern()).contains("%5p");
54+
}
55+
56+
@Test
57+
void whenLogLevelPatternIsSetThenConsoleUsesIt() {
58+
withSystemProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN, "custom",
59+
() -> assertThat(consolePattern()).contains("custom"));
60+
}
61+
62+
@Test
63+
void whenLogLDateformatPatternIsNotConfiguredThenConsoleUsesDefault() {
64+
assertThat(consolePattern()).contains("yyyy-MM-dd HH:mm:ss.SSS");
65+
}
66+
67+
@Test
68+
void whenLogDateformatPatternIsSetThenConsoleUsesIt() {
69+
withSystemProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, "custom",
70+
() -> assertThat(consolePattern()).contains("custom"));
71+
}
72+
73+
protected void withSystemProperty(String name, String value, Runnable action) {
74+
String previous = System.setProperty(name, value);
75+
action.run();
76+
if (previous == null) {
77+
System.clearProperty(name);
78+
}
79+
else {
80+
System.setProperty(name, previous);
81+
}
82+
}
83+
84+
private String consolePattern() {
85+
Configuration configuration = initializeConfiguration();
86+
return ((PatternLayout) configuration.getAppender("Console").getLayout()).getConversionPattern();
87+
}
88+
89+
protected Configuration initializeConfiguration() {
90+
LoggerContext context = new LoggerContext("test");
91+
Configuration configuration = ConfigurationFactory.getInstance().getConfiguration(context,
92+
configurationSource());
93+
configuration.initialize();
94+
return configuration;
95+
}
96+
97+
private ConfigurationSource configurationSource() {
98+
try (InputStream in = getClass().getResourceAsStream(getConfigFileName())) {
99+
return new ConfigurationSource(in);
100+
}
101+
catch (IOException ex) {
102+
throw new RuntimeException(ex);
103+
}
104+
}
105+
106+
protected String getConfigFileName() {
107+
return "log4j2.xml";
108+
}
109+
110+
}

0 commit comments

Comments
 (0)