Skip to content

Commit e1ad564

Browse files
committed
Polish "Trim whitespace when coercing to a LogLevel"
Closes gh-15143
1 parent dfe94a0 commit e1ad564

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,18 +364,19 @@ private void setLogLevel(LoggingSystem system, String[] names, String level) {
364364
private void setLogLevel(LoggingSystem system, String name, String level) {
365365
try {
366366
name = name.equalsIgnoreCase(LoggingSystem.ROOT_LOGGER_NAME) ? null : name;
367-
system.setLogLevel(name, coerceLogLevel(level.trim()));
367+
system.setLogLevel(name, coerceLogLevel(level));
368368
}
369369
catch (RuntimeException ex) {
370-
this.logger.error("Cannot set level: " + level + " for '" + name + "'");
370+
this.logger.error("Cannot set level '" + level + "' for '" + name + "'");
371371
}
372372
}
373373

374374
private LogLevel coerceLogLevel(String level) {
375-
if ("false".equalsIgnoreCase(level)) {
375+
String trimmedLevel = level.trim();
376+
if ("false".equalsIgnoreCase(trimmedLevel)) {
376377
return LogLevel.OFF;
377378
}
378-
return LogLevel.valueOf(level.toUpperCase(Locale.ENGLISH));
379+
return LogLevel.valueOf(trimmedLevel.toUpperCase(Locale.ENGLISH));
379380
}
380381

381382
private void registerShutdownHookIfNecessary(Environment environment,

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,18 @@ public void parseLevelsCaseInsensitive() {
318318
assertThat(this.outputCapture.toString()).contains("testattrace");
319319
}
320320

321+
@Test
322+
public void parseLevelsTrimsWhitespace() {
323+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
324+
"logging.level.org.springframework.boot= trace ");
325+
this.initializer.initialize(this.context.getEnvironment(),
326+
this.context.getClassLoader());
327+
this.logger.debug("testatdebug");
328+
this.logger.trace("testattrace");
329+
assertThat(this.outputCapture.toString()).contains("testatdebug");
330+
assertThat(this.outputCapture.toString()).contains("testattrace");
331+
}
332+
321333
@Test
322334
public void parseLevelsWithPlaceholder() {
323335
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
@@ -338,7 +350,7 @@ public void parseLevelsFails() {
338350
this.context.getClassLoader());
339351
this.logger.debug("testatdebug");
340352
assertThat(this.outputCapture.toString()).doesNotContain("testatdebug")
341-
.contains("Cannot set level: GARBAGE");
353+
.contains("Cannot set level 'GARBAGE'");
342354
}
343355

344356
@Test

0 commit comments

Comments
 (0)