Skip to content

Commit 6fdb6bc

Browse files
committed
Fix http status code when new loglevel is set
See gh-8798
1 parent df6167d commit 6fdb6bc

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.boot.actuate.endpoint.LoggersEndpoint.LoggerLevels;
2323
import org.springframework.boot.context.properties.ConfigurationProperties;
2424
import org.springframework.boot.logging.LogLevel;
25-
import org.springframework.http.HttpEntity;
2625
import org.springframework.http.ResponseEntity;
2726
import org.springframework.web.bind.annotation.PathVariable;
2827
import org.springframework.web.bind.annotation.RequestBody;
@@ -33,6 +32,7 @@
3332
*
3433
* @author Ben Hale
3534
* @author Kazuki Shimizu
35+
* @author Eddú Meléndez
3636
* @since 1.5.0
3737
*/
3838
@ConfigurationProperties(prefix = "endpoints.loggers")
@@ -69,9 +69,14 @@ public Object set(@PathVariable String name,
6969
return getDisabledResponse();
7070
}
7171
String level = configuration.get("configuredLevel");
72-
LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase());
73-
this.delegate.setLogLevel(name, logLevel);
74-
return HttpEntity.EMPTY;
72+
try {
73+
LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase());
74+
this.delegate.setLogLevel(name, logLevel);
75+
return ResponseEntity.ok().build();
76+
}
77+
catch (IllegalArgumentException ex) {
78+
return ResponseEntity.badRequest().build();
79+
}
7580
}
7681

7782
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
*
6565
* @author Ben Hale
6666
* @author Phillip Webb
67+
* @author Eddú Meléndez
6768
*/
6869
@RunWith(SpringRunner.class)
6970
@SpringBootTest
@@ -169,6 +170,14 @@ public void setLoggerWhenDisabledShouldReturnNotFound() throws Exception {
169170
verifyZeroInteractions(this.loggingSystem);
170171
}
171172

173+
@Test
174+
public void setLoggerWithWrongLogLevel() throws Exception {
175+
this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON)
176+
.content("{\"configuredLevel\":\"other\"}"))
177+
.andExpect(status().is4xxClientError());
178+
verifyZeroInteractions(this.loggingSystem);
179+
}
180+
172181
@Configuration
173182
@Import({ JacksonAutoConfiguration.class,
174183
HttpMessageConvertersAutoConfiguration.class,

0 commit comments

Comments
 (0)