|
22 | 22 | import org.springframework.boot.actuate.endpoint.LoggersEndpoint.LoggerLevels;
|
23 | 23 | import org.springframework.boot.context.properties.ConfigurationProperties;
|
24 | 24 | import org.springframework.boot.logging.LogLevel;
|
| 25 | +import org.springframework.http.HttpStatus; |
25 | 26 | import org.springframework.http.ResponseEntity;
|
26 | 27 | import org.springframework.web.bind.annotation.PathVariable;
|
27 | 28 | import org.springframework.web.bind.annotation.RequestBody;
|
28 | 29 | import org.springframework.web.bind.annotation.ResponseBody;
|
| 30 | +import org.springframework.web.bind.annotation.ResponseStatus; |
29 | 31 |
|
30 | 32 | /**
|
31 | 33 | * Adapter to expose {@link LoggersEndpoint} as an {@link MvcEndpoint}.
|
@@ -68,19 +70,32 @@ public Object set(@PathVariable String name,
|
68 | 70 | // disabled
|
69 | 71 | return getDisabledResponse();
|
70 | 72 | }
|
| 73 | + LogLevel logLevel = getLogLevel(configuration); |
| 74 | + this.delegate.setLogLevel(name, logLevel); |
| 75 | + return ResponseEntity.ok().build(); |
| 76 | + } |
| 77 | + |
| 78 | + private LogLevel getLogLevel(Map<String, String> configuration) { |
| 79 | + String level = configuration.get("configuredLevel"); |
71 | 80 | try {
|
72 |
| - LogLevel logLevel = getLogLevel(configuration); |
73 |
| - this.delegate.setLogLevel(name, logLevel); |
74 |
| - return ResponseEntity.ok().build(); |
| 81 | + return (level == null ? null : LogLevel.valueOf(level.toUpperCase())); |
75 | 82 | }
|
76 | 83 | catch (IllegalArgumentException ex) {
|
77 |
| - return ResponseEntity.badRequest().build(); |
| 84 | + throw new InvalidLogLevelException(level); |
78 | 85 | }
|
79 | 86 | }
|
80 | 87 |
|
81 |
| - private LogLevel getLogLevel(Map<String, String> configuration) { |
82 |
| - String level = configuration.get("configuredLevel"); |
83 |
| - return (level == null ? null : LogLevel.valueOf(level.toUpperCase())); |
| 88 | + /** |
| 89 | + * Exception thrown when the specified log level cannot be found. |
| 90 | + */ |
| 91 | + @SuppressWarnings("serial") |
| 92 | + @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "No such log level") |
| 93 | + public static class InvalidLogLevelException extends RuntimeException { |
| 94 | + |
| 95 | + public InvalidLogLevelException(String level) { |
| 96 | + super("Log level '" + level + "' is invalid"); |
| 97 | + } |
| 98 | + |
84 | 99 | }
|
85 | 100 |
|
86 | 101 | }
|
0 commit comments