From cf08162148864cb8064c879c2bff78c87ea1960e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 4 Apr 2017 12:31:08 -0500 Subject: [PATCH 1/2] Fix http status code with unknown log level This commit changes the http status code to 400 when an unknown log level is specified. Closes gh-8798 See gh-8813 --- .../actuate/endpoint/mvc/LoggersMvcEndpoint.java | 13 +++++++++---- .../endpoint/mvc/LoggersMvcEndpointTests.java | 9 +++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java index fc8e15aabf..d582330442 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java @@ -22,7 +22,6 @@ import org.springframework.boot.actuate.endpoint.LoggersEndpoint; import org.springframework.boot.actuate.endpoint.LoggersEndpoint.LoggerLevels; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.logging.LogLevel; -import org.springframework.http.HttpEntity; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; @@ -33,6 +32,7 @@ import org.springframework.web.bind.annotation.ResponseBody; * * @author Ben Hale * @author Kazuki Shimizu + * @author Eddú Meléndez * @since 1.5.0 */ @ConfigurationProperties(prefix = "endpoints.loggers") @@ -69,9 +69,14 @@ public class LoggersMvcEndpoint extends EndpointMvcAdapter { return getDisabledResponse(); } String level = configuration.get("configuredLevel"); - LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); - this.delegate.setLogLevel(name, logLevel); - return HttpEntity.EMPTY; + try { + LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); + this.delegate.setLogLevel(name, logLevel); + return ResponseEntity.ok().build(); + } + catch (IllegalArgumentException ex) { + return ResponseEntity.badRequest().build(); + } } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java index bc67e6421c..ac04663ec6 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java @@ -64,6 +64,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * * @author Ben Hale * @author Phillip Webb + * @author Eddú Meléndez */ @RunWith(SpringRunner.class) @SpringBootTest @@ -169,6 +170,14 @@ public class LoggersMvcEndpointTests { verifyZeroInteractions(this.loggingSystem); } + @Test + public void setLoggerWithWrongLogLevel() throws Exception { + this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON) + .content("{\"configuredLevel\":\"other\"}")) + .andExpect(status().is4xxClientError()); + verifyZeroInteractions(this.loggingSystem); + } + @Configuration @Import({ JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, From 82ac8b5ca13be1de2ebafe0b0d76d8c96aed050e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 11 Apr 2017 13:29:53 +0200 Subject: [PATCH 2/2] Polish "Fix http status code with unknown log level" Closes gh-8813 --- .../boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java index d582330442..c774ce1ab9 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java @@ -68,15 +68,17 @@ public class LoggersMvcEndpoint extends EndpointMvcAdapter { // disabled return getDisabledResponse(); } - String level = configuration.get("configuredLevel"); + LogLevel logLevel; try { - LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); - this.delegate.setLogLevel(name, logLevel); - return ResponseEntity.ok().build(); + String level = configuration.get("configuredLevel"); + logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); } catch (IllegalArgumentException ex) { return ResponseEntity.badRequest().build(); } + + this.delegate.setLogLevel(name, logLevel); + return ResponseEntity.ok().build(); } }