Return log levels in /loggers endpoint payload

Update `LoggersEndpoint` to additionally return the log levels actually
supported by the system.

Fixes gh-7396
This commit is contained in:
Madhura Bhave
2016-11-15 13:57:16 -08:00
committed by Phillip Webb
parent 764f13453a
commit 0e3a3df6f4
13 changed files with 114 additions and 14 deletions

View File

@@ -20,6 +20,9 @@ import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeSet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.logging.LogLevel;
@@ -35,8 +38,7 @@ import org.springframework.util.Assert;
* @since 1.5.0
*/
@ConfigurationProperties(prefix = "endpoints.loggers")
public class LoggersEndpoint
extends AbstractEndpoint<Map<String, LoggersEndpoint.LoggerLevels>> {
public class LoggersEndpoint extends AbstractEndpoint<Map<String, Object>> {
private final LoggingSystem loggingSystem;
@@ -51,18 +53,31 @@ public class LoggersEndpoint
}
@Override
public Map<String, LoggerLevels> invoke() {
public Map<String, Object> invoke() {
Collection<LoggerConfiguration> configurations = this.loggingSystem
.getLoggerConfigurations();
if (configurations == null) {
return Collections.emptyMap();
}
Map<String, LoggerLevels> result = new LinkedHashMap<String, LoggerLevels>(
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("levels", getLevels());
result.put("loggers", getLoggers(configurations));
return result;
}
private NavigableSet<LogLevel> getLevels() {
Set<LogLevel> levels = this.loggingSystem.getSupportedLogLevels();
return new TreeSet<LogLevel>(levels).descendingSet();
}
private Map<String, LoggerLevels> getLoggers(
Collection<LoggerConfiguration> configurations) {
Map<String, LoggerLevels> loggers = new LinkedHashMap<String, LoggerLevels>(
configurations.size());
for (LoggerConfiguration configuration : configurations) {
result.put(configuration.getName(), new LoggerLevels(configuration));
loggers.put(configuration.getName(), new LoggerLevels(configuration));
}
return result;
return loggers;
}
public LoggerLevels invoke(String name) {

View File

@@ -130,7 +130,9 @@ public class EndpointAutoConfigurationTests {
public void loggersEndpointHasLoggers() throws Exception {
load(CustomLoggingConfig.class, EndpointAutoConfiguration.class);
LoggersEndpoint endpoint = this.context.getBean(LoggersEndpoint.class);
Map<String, LoggerLevels> loggers = endpoint.invoke();
Map<String, Object> result = endpoint.invoke();
Map<String, LoggerLevels> loggers = (Map<String, LoggerLevels>) result
.get("loggers");
assertThat(loggers.size()).isGreaterThan(0);
}

View File

@@ -17,6 +17,9 @@
package org.springframework.boot.actuate.endpoint;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
@@ -45,12 +48,21 @@ public class LoggersEndpointTests extends AbstractEndpointTests<LoggersEndpoint>
}
@Test
@SuppressWarnings("unchecked")
public void invokeShouldReturnConfigurations() throws Exception {
given(getLoggingSystem().getLoggerConfigurations()).willReturn(Collections
.singletonList(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG)));
LoggerLevels levels = getEndpointBean().invoke().get("ROOT");
assertThat(levels.getConfiguredLevel()).isNull();
assertThat(levels.getEffectiveLevel()).isEqualTo("DEBUG");
given(getLoggingSystem().getSupportedLogLevels())
.willReturn(EnumSet.allOf(LogLevel.class));
Map<String, Object> result = getEndpointBean().invoke();
Map<String, LoggerLevels> loggers = (Map<String, LoggerLevels>) result
.get("loggers");
Set<LogLevel> levels = (Set<LogLevel>) result.get("levels");
LoggerLevels rootLevels = loggers.get("ROOT");
assertThat(rootLevels.getConfiguredLevel()).isNull();
assertThat(rootLevels.getEffectiveLevel()).isEqualTo("DEBUG");
assertThat(levels).containsExactly(LogLevel.OFF, LogLevel.FATAL, LogLevel.ERROR,
LogLevel.WARN, LogLevel.INFO, LogLevel.DEBUG, LogLevel.TRACE);
}
public void invokeWhenNameSpecifiedShouldReturnLevels() throws Exception {

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.actuate.endpoint.mvc;
import java.util.Collections;
import java.util.EnumSet;
import org.junit.After;
import org.junit.Before;
@@ -80,18 +81,23 @@ public class LoggersMvcEndpointTests {
.alwaysDo(MockMvcResultHandlers.print()).build();
}
@Before
@After
public void reset() {
public void resetMocks() {
Mockito.reset(this.loggingSystem);
given(this.loggingSystem.getSupportedLogLevels())
.willReturn(EnumSet.allOf(LogLevel.class));
}
@Test
public void getLoggerShouldReturnAllLoggerConfigurations() throws Exception {
given(this.loggingSystem.getLoggerConfigurations()).willReturn(Collections
.singletonList(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG)));
String expected = "{\"levels\":[\"OFF\",\"FATAL\",\"ERROR\",\"WARN\",\"INFO\",\"DEBUG\",\"TRACE\"],"
+ "\"loggers\":{\"ROOT\":{\"configuredLevel\":null,\"effectiveLevel\":\"DEBUG\"}}}";
System.out.println(expected);
this.mvc.perform(get("/loggers")).andExpect(status().isOk())
.andExpect(content().string(equalTo("{\"ROOT\":{\"configuredLevel\":"
+ "null,\"effectiveLevel\":\"DEBUG\"}}")));
.andExpect(content().json(expected));
}
@Test