Skip to content

Commit a07f027

Browse files
committed
Polish
1 parent 04c3e99 commit a07f027

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

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

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,19 @@ public void onApplicationEvent(ApplicationEvent event) {
141141
* {@link Environment} and the classpath.
142142
*/
143143
protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) {
144+
initializeEarlyLoggingLevel(environment);
145+
cleanLogTempProperty();
146+
LoggingSystem system = LoggingSystem.get(classLoader);
147+
boolean systemEnvironmentChanged = mapSystemPropertiesFromSpring(environment);
148+
if (systemEnvironmentChanged) {
149+
// Re-initialize the defaults in case the system Environment changed
150+
system.beforeInitialize();
151+
}
152+
initializeSystem(environment, system);
153+
initializeFinalLoggingLevels(environment, system);
154+
}
144155

156+
private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
145157
if (this.parseArgs && this.springBootLogging == null) {
146158
if (environment.containsProperty("debug")) {
147159
this.springBootLogging = LogLevel.DEBUG;
@@ -150,7 +162,9 @@ protected void initialize(ConfigurableEnvironment environment, ClassLoader class
150162
this.springBootLogging = LogLevel.TRACE;
151163
}
152164
}
165+
}
153166

167+
private void cleanLogTempProperty() {
154168
// Logback won't read backslashes so add a clean path for it to use
155169
if (!StringUtils.hasLength(System.getProperty("LOG_TEMP"))) {
156170
String path = System.getProperty("java.io.tmpdir");
@@ -160,47 +174,48 @@ protected void initialize(ConfigurableEnvironment environment, ClassLoader class
160174
}
161175
System.setProperty("LOG_TEMP", path);
162176
}
177+
}
163178

164-
boolean environmentChanged = false;
179+
private boolean mapSystemPropertiesFromSpring(Environment environment) {
180+
boolean changed = false;
165181
for (Map.Entry<String, String> mapping : ENVIRONMENT_SYSTEM_PROPERTY_MAPPING
166182
.entrySet()) {
167-
if (environment.containsProperty(mapping.getKey())) {
168-
System.setProperty(mapping.getValue(),
169-
environment.getProperty(mapping.getKey()));
170-
environmentChanged = true;
183+
String springName = mapping.getKey();
184+
String systemName = mapping.getValue();
185+
if (environment.containsProperty(springName)) {
186+
System.setProperty(systemName, environment.getProperty(springName));
187+
changed = true;
171188
}
172189
}
190+
return changed;
191+
}
173192

174-
LoggingSystem system = LoggingSystem.get(classLoader);
175-
176-
if (environmentChanged) {
177-
// Re-initialize the defaults in case the Environment changed
178-
system.beforeInitialize();
179-
}
180-
// User specified configuration
193+
private void initializeSystem(ConfigurableEnvironment environment,
194+
LoggingSystem system) {
181195
if (environment.containsProperty("logging.config")) {
182196
String value = environment.getProperty("logging.config");
183197
try {
184198
ResourceUtils.getURL(value).openStream().close();
185199
system.initialize(value);
186200
}
187201
catch (Exception ex) {
188-
this.logger
189-
.warn("Logging environment value '"
190-
+ value
191-
+ "' cannot be opened and will be ignored (using default location instead)");
202+
this.logger.warn("Logging environment value '" + value
203+
+ "' cannot be opened and will be ignored "
204+
+ "(using default location instead)");
192205
system.initialize();
193206
}
194207
}
195208
else {
196209
system.initialize();
197210
}
211+
}
198212

213+
private void initializeFinalLoggingLevels(ConfigurableEnvironment environment,
214+
LoggingSystem system) {
199215
if (this.springBootLogging != null) {
200216
initializeLogLevel(system, this.springBootLogging);
201217
}
202218
setLogLevels(system, environment);
203-
204219
}
205220

206221
public void setLogLevels(LoggingSystem system, Environment environment) {

0 commit comments

Comments
 (0)