Skip to content

Commit ddfadce

Browse files
committed
Set all documented system properties in LoggingApplicationListener
Closes gh-5073
1 parent 4cbbd48 commit ddfadce

File tree

5 files changed

+77
-25
lines changed

5 files changed

+77
-25
lines changed

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

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ public class LoggingApplicationListener implements GenericApplicationListener {
109109
*/
110110
public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD";
111111

112+
/**
113+
* The name of the System property that contains the log file.
114+
*/
115+
public static final String LOG_FILE = "LOG_FILE";
116+
117+
/**
118+
* The name of the System property that contains the log file.
119+
*/
120+
public static final String LOG_PATH = "LOG_PATH";
121+
122+
/**
123+
* The name of the System property that contains the console log pattern
124+
*/
125+
public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN";
126+
127+
/**
128+
* The name of the System property that contains the file log pattern
129+
*/
130+
public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN";
131+
132+
/**
133+
* The name of the System property that contains the log level pattern
134+
*/
135+
public static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN";
136+
112137
/**
113138
* The name of the {@link LoggingSystem} bean.
114139
*/
@@ -222,23 +247,38 @@ private void onContextClosedEvent() {
222247
*/
223248
protected void initialize(ConfigurableEnvironment environment,
224249
ClassLoader classLoader) {
225-
if (System.getProperty(PID_KEY) == null) {
226-
System.setProperty(PID_KEY, new ApplicationPid().toString());
227-
}
228-
if (System.getProperty(EXCEPTION_CONVERSION_WORD) == null) {
229-
System.setProperty(EXCEPTION_CONVERSION_WORD,
230-
getExceptionConversionWord(environment));
231-
}
250+
LogFile logFile = LogFile.get(environment);
251+
setSystemProperties(environment, logFile);
232252
initializeEarlyLoggingLevel(environment);
233-
initializeSystem(environment, this.loggingSystem);
253+
initializeSystem(environment, this.loggingSystem, logFile);
234254
initializeFinalLoggingLevels(environment, this.loggingSystem);
235255
registerShutdownHookIfNecessary(environment, this.loggingSystem);
236256
}
237257

238-
private String getExceptionConversionWord(ConfigurableEnvironment environment) {
239-
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
240-
"logging.");
241-
return resolver.getProperty("exception-conversion-word", "%wEx");
258+
private void setSystemProperties(ConfigurableEnvironment environment,
259+
LogFile logFile) {
260+
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(
261+
environment, "logging.");
262+
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
263+
"exception-conversion-word");
264+
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");
265+
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
266+
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
267+
setSystemProperty(PID_KEY, new ApplicationPid().toString());
268+
if (logFile != null) {
269+
logFile.applyToSystemProperties();
270+
}
271+
}
272+
273+
private void setSystemProperty(RelaxedPropertyResolver propertyResolver,
274+
String systemPropertyName, String propertyName) {
275+
setSystemProperty(systemPropertyName, propertyResolver.getProperty(propertyName));
276+
}
277+
278+
private void setSystemProperty(String name, String value) {
279+
if (System.getProperty(name) == null && value != null) {
280+
System.setProperty(name, value);
281+
}
242282
}
243283

244284
private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
@@ -253,10 +293,9 @@ private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
253293
}
254294

255295
private void initializeSystem(ConfigurableEnvironment environment,
256-
LoggingSystem system) {
296+
LoggingSystem system, LogFile logFile) {
257297
LoggingInitializationContext initializationContext = new LoggingInitializationContext(
258298
environment);
259-
LogFile logFile = LogFile.get(environment);
260299
String logConfig = environment.getProperty(CONFIG_PROPERTY);
261300
if (ignoreLogConfig(logConfig)) {
262301
system.initialize(initializationContext, null, logFile);

spring-boot/src/main/java/org/springframework/boot/logging/log4j/Log4JLoggingSystem.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -93,9 +93,6 @@ protected void loadConfiguration(LoggingInitializationContext initializationCont
9393

9494
protected void loadConfiguration(String location, LogFile logFile) {
9595
Assert.notNull(location, "Location must not be null");
96-
if (logFile != null) {
97-
logFile.applyToSystemProperties();
98-
}
9996
try {
10097
Log4jConfigurer.initLogging(location);
10198
}

spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ protected void loadConfiguration(LoggingInitializationContext initializationCont
157157

158158
protected void loadConfiguration(String location, LogFile logFile) {
159159
Assert.notNull(location, "Location must not be null");
160-
if (logFile != null) {
161-
logFile.applyToSystemProperties();
162-
}
163160
try {
164161
LoggerContext ctx = getLoggerContext();
165162
URL url = ResourceUtils.getURL(location);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ protected void loadDefaults(LoggingInitializationContext initializationContext,
128128
protected void loadConfiguration(LoggingInitializationContext initializationContext,
129129
String location, LogFile logFile) {
130130
Assert.notNull(location, "Location must not be null");
131-
if (logFile != null) {
132-
logFile.applyToSystemProperties();
133-
}
134131
LoggerContext loggerContext = getLoggerContext();
135132
stopAndReset(loggerContext);
136133
try {

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -45,6 +45,7 @@
4545
import org.springframework.test.util.ReflectionTestUtils;
4646

4747
import static org.hamcrest.Matchers.containsString;
48+
import static org.hamcrest.Matchers.equalTo;
4849
import static org.hamcrest.Matchers.is;
4950
import static org.hamcrest.Matchers.not;
5051
import static org.hamcrest.Matchers.nullValue;
@@ -98,6 +99,9 @@ public void clear() {
9899
System.clearProperty("LOG_PATH");
99100
System.clearProperty("PID");
100101
System.clearProperty("LOG_EXCEPTION_CONVERSION_WORD");
102+
System.clearProperty("CONSOLE_LOG_PATTERN");
103+
System.clearProperty("FILE_LOG_PATTERN");
104+
System.clearProperty("LOG_LEVEL_PATTERN");
101105
System.clearProperty(LoggingSystem.SYSTEM_PROPERTY);
102106
if (this.context != null) {
103107
this.context.close();
@@ -419,6 +423,24 @@ public void closingChildContextDoesNotCleanUpLoggingSystem() {
419423
childContext.close();
420424
}
421425

426+
@Test
427+
public void systemPropertiesAreSetForLoggingConfiguration() {
428+
EnvironmentTestUtils.addEnvironment(this.context,
429+
"logging.exception-conversion-word=conversion", "logging.file=file",
430+
"logging.path=path", "logging.pattern.console=console",
431+
"logging.pattern.file=file", "logging.pattern.level=level");
432+
this.initializer.initialize(this.context.getEnvironment(),
433+
this.context.getClassLoader());
434+
assertThat(System.getProperty("CONSOLE_LOG_PATTERN"), is(equalTo("console")));
435+
assertThat(System.getProperty("FILE_LOG_PATTERN"), is(equalTo("file")));
436+
assertThat(System.getProperty("LOG_EXCEPTION_CONVERSION_WORD"),
437+
is(equalTo("conversion")));
438+
assertThat(System.getProperty("LOG_FILE"), is(equalTo("file")));
439+
assertThat(System.getProperty("LOG_LEVEL_PATTERN"), is(equalTo("level")));
440+
assertThat(System.getProperty("LOG_PATH"), is(equalTo("path")));
441+
assertThat(System.getProperty("PID"), is(not(nullValue())));
442+
}
443+
422444
private boolean bridgeHandlerInstalled() {
423445
Logger rootLogger = LogManager.getLogManager().getLogger("");
424446
Handler[] handlers = rootLogger.getHandlers();

0 commit comments

Comments
 (0)