Skip to content

Commit 07313d1

Browse files
committed
Merge branch '1.3.x'
2 parents d66ff0e + 0a7a283 commit 07313d1

8 files changed

Lines changed: 125 additions & 35 deletions

File tree

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

Lines changed: 6 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.logging;
1818

19+
import org.springframework.core.env.Environment;
1920
import org.springframework.core.io.ClassPathResource;
2021
import org.springframework.util.ClassUtils;
2122
import org.springframework.util.StringUtils;
@@ -168,4 +169,8 @@ protected final String getPackagedConfigFile(String fileName) {
168169
return defaultPath;
169170
}
170171

172+
protected final void applySystemProperties(Environment environment, LogFile logFile) {
173+
new LoggingSytemProperties(environment).apply(logFile);
174+
}
175+
171176
}

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

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.apache.commons.logging.LogFactory;
2626

2727
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
28-
import org.springframework.boot.ApplicationPid;
2928
import org.springframework.boot.SpringApplication;
3029
import org.springframework.boot.bind.RelaxedPropertyResolver;
3130
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
@@ -103,12 +102,12 @@ public class LoggingApplicationListener implements GenericApplicationListener {
103102
/**
104103
* The name of the System property that contains the process ID.
105104
*/
106-
public static final String PID_KEY = "PID";
105+
public static final String PID_KEY = LoggingSytemProperties.PID_KEY;
107106

108107
/**
109108
* The name of the System property that contains the exception conversion word.
110109
*/
111-
public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD";
110+
public static final String EXCEPTION_CONVERSION_WORD = LoggingSytemProperties.EXCEPTION_CONVERSION_WORD;
112111

113112
/**
114113
* The name of the System property that contains the log file.
@@ -123,17 +122,17 @@ public class LoggingApplicationListener implements GenericApplicationListener {
123122
/**
124123
* The name of the System property that contains the console log pattern.
125124
*/
126-
public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN";
125+
public static final String CONSOLE_LOG_PATTERN = LoggingSytemProperties.CONSOLE_LOG_PATTERN;
127126

128127
/**
129128
* The name of the System property that contains the file log pattern.
130129
*/
131-
public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN";
130+
public static final String FILE_LOG_PATTERN = LoggingSytemProperties.FILE_LOG_PATTERN;
132131

133132
/**
134133
* The name of the System property that contains the log level pattern.
135134
*/
136-
public static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN";
135+
public static final String LOG_LEVEL_PATTERN = LoggingSytemProperties.LOG_LEVEL_PATTERN;
137136

138137
/**
139138
* The name of the {@link LoggingSystem} bean.
@@ -248,7 +247,7 @@ private void onContextClosedEvent() {
248247
*/
249248
protected void initialize(ConfigurableEnvironment environment,
250249
ClassLoader classLoader) {
251-
setSystemProperties(environment);
250+
new LoggingSytemProperties(environment).apply();
252251
LogFile logFile = LogFile.get(environment);
253252
if (logFile != null) {
254253
logFile.applyToSystemProperties();
@@ -259,28 +258,6 @@ protected void initialize(ConfigurableEnvironment environment,
259258
registerShutdownHookIfNecessary(environment, this.loggingSystem);
260259
}
261260

262-
private void setSystemProperties(ConfigurableEnvironment environment) {
263-
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(
264-
environment, "logging.");
265-
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
266-
"exception-conversion-word");
267-
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");
268-
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
269-
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
270-
setSystemProperty(PID_KEY, new ApplicationPid().toString());
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-
}
282-
}
283-
284261
private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
285262
if (this.parseArgs && this.springBootLogging == null) {
286263
if (isSet(environment, "debug")) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2012-2016 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+
* http://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;
18+
19+
import org.springframework.boot.ApplicationPid;
20+
import org.springframework.boot.bind.RelaxedPropertyResolver;
21+
import org.springframework.core.env.Environment;
22+
23+
/**
24+
* Utility to set system properties that can later be used by log configuration files.
25+
*
26+
* @author Andy Wilkinson
27+
* @author Phillip Webb
28+
*/
29+
class LoggingSytemProperties {
30+
31+
static final String PID_KEY = "PID";
32+
33+
static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD";
34+
35+
static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN";
36+
37+
static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN";
38+
39+
static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN";
40+
41+
private final Environment environment;
42+
43+
LoggingSytemProperties(Environment environment) {
44+
this.environment = environment;
45+
}
46+
47+
public void apply() {
48+
apply(null);
49+
}
50+
51+
public void apply(LogFile logFile) {
52+
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(
53+
this.environment, "logging.");
54+
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
55+
"exception-conversion-word");
56+
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");
57+
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
58+
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
59+
setSystemProperty(PID_KEY, new ApplicationPid().toString());
60+
if (logFile != null) {
61+
logFile.applyToSystemProperties();
62+
}
63+
}
64+
65+
private void setSystemProperty(RelaxedPropertyResolver propertyResolver,
66+
String systemPropertyName, String propertyName) {
67+
setSystemProperty(systemPropertyName, propertyResolver.getProperty(propertyName));
68+
}
69+
70+
private void setSystemProperty(String name, String value) {
71+
if (System.getProperty(name) == null && value != null) {
72+
System.setProperty(name, value);
73+
}
74+
}
75+
76+
}

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

Lines changed: 11 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.
@@ -18,6 +18,7 @@
1818

1919
import org.slf4j.bridge.SLF4JBridgeHandler;
2020

21+
import org.springframework.util.Assert;
2122
import org.springframework.util.ClassUtils;
2223

2324
/**
@@ -45,6 +46,15 @@ public void cleanUp() {
4546
removeJdkLoggingBridgeHandler();
4647
}
4748

49+
@Override
50+
protected void loadConfiguration(LoggingInitializationContext initializationContext,
51+
String location, LogFile logFile) {
52+
Assert.notNull(location, "Location must not be null");
53+
if (initializationContext != null) {
54+
applySystemProperties(initializationContext.getEnvironment(), logFile);
55+
}
56+
}
57+
4858
private void configureJdkLoggingBridgeHandler() {
4959
try {
5060
if (isBridgeHandlerAvailable()) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ protected void loadDefaults(LoggingInitializationContext initializationContext,
152152
@Override
153153
protected void loadConfiguration(LoggingInitializationContext initializationContext,
154154
String location, LogFile logFile) {
155+
super.loadConfiguration(initializationContext, location, logFile);
155156
loadConfiguration(location, logFile);
156157
}
157158

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected void loadDefaults(LoggingInitializationContext initializationContext,
128128
@Override
129129
protected void loadConfiguration(LoggingInitializationContext initializationContext,
130130
String location, LogFile logFile) {
131-
Assert.notNull(location, "Location must not be null");
131+
super.loadConfiguration(initializationContext, location, logFile);
132132
LoggerContext loggerContext = getLoggerContext();
133133
stopAndReset(loggerContext);
134134
try {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,15 @@ protected final String[] getSpringConfigLocations(AbstractLoggingSystem system)
6262
}
6363

6464
protected final LogFile getLogFile(String file, String path) {
65+
return getLogFile(file, path, true);
66+
}
67+
68+
protected final LogFile getLogFile(String file, String path,
69+
boolean applyToSystemProperties) {
6570
LogFile logFile = new LogFile(file, path);
66-
logFile.applyToSystemProperties();
71+
if (applyToSystemProperties) {
72+
logFile.applyToSystemProperties();
73+
}
6774
return logFile;
6875
}
6976

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
6868

6969
private LoggingInitializationContext initializationContext;
7070

71+
private MockEnvironment environment;
72+
7173
@Before
7274
public void setup() {
7375
this.logger = new SLF4JLogFactory().getInstance(getClass().getName());
74-
this.initializationContext = new LoggingInitializationContext(
75-
new MockEnvironment());
76+
this.environment = new MockEnvironment();
77+
this.initializationContext = new LoggingInitializationContext(this.environment);
7678
}
7779

7880
@Override
@@ -301,6 +303,18 @@ public void customExceptionConversionWord() throws Exception {
301303
}
302304
}
303305

306+
@Test
307+
public void reinitializeShouldSetSytemProperty() throws Exception {
308+
// gh-5491
309+
this.loggingSystem.beforeInitialize();
310+
this.logger.info("Hidden");
311+
this.loggingSystem.initialize(this.initializationContext, null, null);
312+
LogFile logFile = getLogFile(tmpDir() + "/example.log", null, false);
313+
this.loggingSystem.initialize(this.initializationContext,
314+
"classpath:logback-nondefault.xml", logFile);
315+
assertThat(System.getProperty("LOG_FILE")).endsWith("example.log");
316+
}
317+
304318
private String getLineWithText(File file, String outputSearch) throws Exception {
305319
return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)),
306320
outputSearch);

0 commit comments

Comments
 (0)