Skip to content

Commit 0b0fe89

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

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
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.noContent().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: 11 additions & 2 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
@@ -148,15 +149,15 @@ public void contentTypeForGetCanBeApplicationJson() throws Exception {
148149
@Test
149150
public void setLoggerUsingApplicationJsonShouldSetLogLevel() throws Exception {
150151
this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON)
151-
.content("{\"configuredLevel\":\"debug\"}")).andExpect(status().isOk());
152+
.content("{\"configuredLevel\":\"debug\"}")).andExpect(status().isNoContent());
152153
verify(this.loggingSystem).setLogLevel("ROOT", LogLevel.DEBUG);
153154
}
154155

155156
@Test
156157
public void setLoggerUsingActuatorV1JsonShouldSetLogLevel() throws Exception {
157158
this.mvc.perform(post("/loggers/ROOT")
158159
.contentType(ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON)
159-
.content("{\"configuredLevel\":\"debug\"}")).andExpect(status().isOk());
160+
.content("{\"configuredLevel\":\"debug\"}")).andExpect(status().isNoContent());
160161
verify(this.loggingSystem).setLogLevel("ROOT", LogLevel.DEBUG);
161162
}
162163

@@ -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)