Polish `/loggers` actuator endpoint

See gh-7086
pull/7225/head
Phillip Webb 8 years ago
parent 06cb4fcca5
commit a448183681

@ -31,10 +31,12 @@ import org.springframework.util.Assert;
* {@link Endpoint} to expose a collection of {@link LoggerConfiguration}s. * {@link Endpoint} to expose a collection of {@link LoggerConfiguration}s.
* *
* @author Ben Hale * @author Ben Hale
* @author Phillip Webb
* @since 1.5.0 * @since 1.5.0
*/ */
@ConfigurationProperties(prefix = "endpoints.loggers") @ConfigurationProperties(prefix = "endpoints.loggers")
public class LoggersEndpoint extends AbstractEndpoint<Map<String, Map<String, String>>> { public class LoggersEndpoint
extends AbstractEndpoint<Map<String, LoggersEndpoint.LoggerLevels>> {
private final LoggingSystem loggingSystem; private final LoggingSystem loggingSystem;
@ -49,44 +51,58 @@ public class LoggersEndpoint extends AbstractEndpoint<Map<String, Map<String, St
} }
@Override @Override
public Map<String, Map<String, String>> invoke() { public Map<String, LoggerLevels> invoke() {
Collection<LoggerConfiguration> loggerConfigurations = this.loggingSystem Collection<LoggerConfiguration> configurations = this.loggingSystem
.listLoggerConfigurations(); .getLoggerConfigurations();
if (configurations == null) {
if (loggerConfigurations == null) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
Map<String, LoggerLevels> result = new LinkedHashMap<String, LoggerLevels>(
Map<String, Map<String, String>> result = new LinkedHashMap<String, configurations.size());
Map<String, String>>(loggerConfigurations.size()); for (LoggerConfiguration configuration : configurations) {
result.put(configuration.getName(), new LoggerLevels(configuration));
for (LoggerConfiguration loggerConfiguration : loggerConfigurations) {
result.put(loggerConfiguration.getName(), result(loggerConfiguration));
} }
return result; return result;
} }
public Map<String, String> get(String name) { public LoggerLevels invoke(String name) {
Assert.notNull(name, "Name must not be null"); Assert.notNull(name, "Name must not be null");
return result(this.loggingSystem.getLoggerConfiguration(name)); LoggerConfiguration configuration = this.loggingSystem
.getLoggerConfiguration(name);
return (configuration == null ? null : new LoggerLevels(configuration));
} }
public void set(String name, LogLevel level) { public void setLogLevel(String name, LogLevel level) {
Assert.notNull(name, "Name must not be empty"); Assert.notNull(name, "Name must not be empty");
this.loggingSystem.setLogLevel(name, level); this.loggingSystem.setLogLevel(name, level);
} }
private static Map<String, String> result(LoggerConfiguration loggerConfiguration) { /**
if (loggerConfiguration == null) { * Levels configured for a given logger exposed in a JSON friendly way.
return Collections.emptyMap(); */
public static class LoggerLevels {
private String configuredLevel;
private String effectiveLevel;
public LoggerLevels(LoggerConfiguration configuration) {
this.configuredLevel = getName(configuration.getConfiguredLevel());
this.effectiveLevel = getName(configuration.getEffectiveLevel());
} }
Map<String, String> result = new LinkedHashMap<String, String>(3);
LogLevel configuredLevel = loggerConfiguration.getConfiguredLevel(); private String getName(LogLevel level) {
result.put("configuredLevel", return (level == null ? null : level.name());
configuredLevel != null ? configuredLevel.name() : null); }
result.put("effectiveLevel", loggerConfiguration.getEffectiveLevel().name());
return result; public String getConfiguredLevel() {
return this.configuredLevel;
}
public String getEffectiveLevel() {
return this.effectiveLevel;
}
} }
} }

@ -19,8 +19,10 @@ package org.springframework.boot.actuate.endpoint.mvc;
import java.util.Map; import java.util.Map;
import org.springframework.boot.actuate.endpoint.LoggersEndpoint; 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.context.properties.ConfigurationProperties;
import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LogLevel;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -54,11 +56,11 @@ public class LoggersMvcEndpoint extends EndpointMvcAdapter {
// disabled // disabled
return getDisabledResponse(); return getDisabledResponse();
} }
return this.delegate.get(name); LoggerLevels levels = this.delegate.invoke(name);
return (levels == null ? ResponseEntity.notFound().build() : levels);
} }
@PostMapping(value = "/{name:.*}", consumes = MediaType.APPLICATION_JSON_VALUE, @PostMapping(value = "/{name:.*}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody @ResponseBody
@HypermediaDisabled @HypermediaDisabled
public Object set(@PathVariable String name, public Object set(@PathVariable String name,
@ -69,8 +71,8 @@ public class LoggersMvcEndpoint extends EndpointMvcAdapter {
return getDisabledResponse(); return getDisabledResponse();
} }
String level = configuration.get("configuredLevel"); String level = configuration.get("configuredLevel");
this.delegate.set(name, level == null ? null : LogLevel.valueOf(level)); this.delegate.setLogLevel(name, level == null ? null : LogLevel.valueOf(level));
return ResponseEntity.EMPTY; return HttpEntity.EMPTY;
} }
} }

@ -37,6 +37,7 @@ import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.InfoEndpoint; import org.springframework.boot.actuate.endpoint.InfoEndpoint;
import org.springframework.boot.actuate.endpoint.LiquibaseEndpoint; import org.springframework.boot.actuate.endpoint.LiquibaseEndpoint;
import org.springframework.boot.actuate.endpoint.LoggersEndpoint; import org.springframework.boot.actuate.endpoint.LoggersEndpoint;
import org.springframework.boot.actuate.endpoint.LoggersEndpoint.LoggerLevels;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint; import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.PublicMetrics; import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint; import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
@ -129,7 +130,7 @@ public class EndpointAutoConfigurationTests {
public void loggersEndpointHasLoggers() throws Exception { public void loggersEndpointHasLoggers() throws Exception {
load(CustomLoggingConfig.class, EndpointAutoConfiguration.class); load(CustomLoggingConfig.class, EndpointAutoConfiguration.class);
LoggersEndpoint endpoint = this.context.getBean(LoggersEndpoint.class); LoggersEndpoint endpoint = this.context.getBean(LoggersEndpoint.class);
Map<String, Map<String, String>> loggers = endpoint.invoke(); Map<String, LoggerLevels> loggers = endpoint.invoke();
assertThat(loggers.size()).isGreaterThan(0); assertThat(loggers.size()).isGreaterThan(0);
} }

@ -17,10 +17,10 @@
package org.springframework.boot.actuate.endpoint; package org.springframework.boot.actuate.endpoint;
import java.util.Collections; import java.util.Collections;
import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.endpoint.LoggersEndpoint.LoggerLevels;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggerConfiguration;
@ -45,25 +45,24 @@ public class LoggersEndpointTests extends AbstractEndpointTests<LoggersEndpoint>
} }
@Test @Test
public void invoke() throws Exception { public void invokeShouldReturnConfigurations() throws Exception {
given(getLoggingSystem().listLoggerConfigurations()).willReturn(Collections given(getLoggingSystem().getLoggerConfigurations()).willReturn(Collections
.singleton(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG))); .singletonList(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG)));
Map<String, String> loggingConfiguration = getEndpointBean().invoke() LoggerLevels levels = getEndpointBean().invoke().get("ROOT");
.get("ROOT"); assertThat(levels.getConfiguredLevel()).isNull();
assertThat(loggingConfiguration.get("configuredLevel")).isNull(); assertThat(levels.getEffectiveLevel()).isEqualTo("DEBUG");
assertThat(loggingConfiguration.get("effectiveLevel")).isEqualTo("DEBUG");
} }
public void get() throws Exception { public void invokeWhenNameSpecifiedShouldReturnLevels() throws Exception {
given(getLoggingSystem().getLoggerConfiguration("ROOT")) given(getLoggingSystem().getLoggerConfiguration("ROOT"))
.willReturn(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG)); .willReturn(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG));
Map<String, String> loggingConfiguration = getEndpointBean().get("ROOT"); LoggerLevels levels = getEndpointBean().invoke("ROOT");
assertThat(loggingConfiguration.get("configuredLevel")).isNull(); assertThat(levels.getConfiguredLevel()).isNull();
assertThat(loggingConfiguration.get("effectiveLevel")).isEqualTo("DEBUG"); assertThat(levels.getEffectiveLevel()).isEqualTo("DEBUG");
} }
public void set() throws Exception { public void setLogLevelShouldSetLevelOnLoggingSystem() throws Exception {
getEndpointBean().set("ROOT", LogLevel.DEBUG); getEndpointBean().setLogLevel("ROOT", LogLevel.DEBUG);
verify(getLoggingSystem()).setLogLevel("ROOT", LogLevel.DEBUG); verify(getLoggingSystem()).setLogLevel("ROOT", LogLevel.DEBUG);
} }

@ -18,9 +18,11 @@ package org.springframework.boot.actuate.endpoint.mvc;
import java.util.Collections; import java.util.Collections;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration;
@ -37,9 +39,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
@ -57,9 +59,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* Tests for {@link LoggersMvcEndpoint}. * Tests for {@link LoggersMvcEndpoint}.
* *
* @author Ben Hale * @author Ben Hale
* @author Phillip Webb
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DirtiesContext
@SpringBootTest @SpringBootTest
public class LoggersMvcEndpointTests { public class LoggersMvcEndpointTests {
@ -74,50 +76,60 @@ public class LoggersMvcEndpointTests {
@Before @Before
public void setUp() { public void setUp() {
this.context.getBean(LoggersEndpoint.class).setEnabled(true); this.context.getBean(LoggersEndpoint.class).setEnabled(true);
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); this.mvc = MockMvcBuilders.webAppContextSetup(this.context)
.alwaysDo(MockMvcResultHandlers.print()).build();
} }
@Test @After
public void list() throws Exception { public void reset() {
given(this.loggingSystem.listLoggerConfigurations()).willReturn(Collections Mockito.reset(this.loggingSystem);
.singleton(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG))); }
@Test
public void getLoggerShouldReturnAllLoggerConfigurations() throws Exception {
given(this.loggingSystem.getLoggerConfigurations()).willReturn(Collections
.singletonList(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG)));
this.mvc.perform(get("/loggers")).andExpect(status().isOk()) this.mvc.perform(get("/loggers")).andExpect(status().isOk())
.andExpect(content().string(equalTo("{\"ROOT\":{\"configuredLevel\":" .andExpect(content().string(equalTo("{\"ROOT\":{\"configuredLevel\":"
+ "null,\"effectiveLevel\":\"DEBUG\"}}"))); + "null,\"effectiveLevel\":\"DEBUG\"}}")));
} }
@Test @Test
public void listDisabled() throws Exception { public void getLoggersWhenDisabledShouldReturnNotFound() throws Exception {
this.context.getBean(LoggersEndpoint.class).setEnabled(false); this.context.getBean(LoggersEndpoint.class).setEnabled(false);
this.mvc.perform(get("/loggers")).andExpect(status().isNotFound()); this.mvc.perform(get("/loggers")).andExpect(status().isNotFound());
} }
@Test @Test
public void getLogger() throws Exception { public void getLoggerShouldReturnLogLevels() throws Exception {
given(this.loggingSystem.getLoggerConfiguration("ROOT")) given(this.loggingSystem.getLoggerConfiguration("ROOT"))
.willReturn(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG)); .willReturn(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG));
this.mvc.perform(get("/loggers/ROOT")).andExpect(status().isOk()) this.mvc.perform(get("/loggers/ROOT")).andExpect(status().isOk())
.andExpect(content().string(equalTo("{\"configuredLevel\":null," .andExpect(content().string(equalTo(
+ "\"effectiveLevel\":\"DEBUG\"}"))); "{\"configuredLevel\":null," + "\"effectiveLevel\":\"DEBUG\"}")));
} }
@Test @Test
public void getLoggerDisabled() throws Exception { public void getLoggesWhenDisabledShouldReturnNotFound() throws Exception {
this.context.getBean(LoggersEndpoint.class).setEnabled(false); this.context.getBean(LoggersEndpoint.class).setEnabled(false);
this.mvc.perform(get("/loggers/ROOT")).andExpect(status().isNotFound()); this.mvc.perform(get("/loggers/ROOT")).andExpect(status().isNotFound());
} }
@Test @Test
public void setLogger() throws Exception { public void getLoggersWhenLoggerNotFoundShouldReturnNotFound() throws Exception {
this.mvc.perform(get("/loggers/com.does.not.exist"))
.andExpect(status().isNotFound());
}
@Test
public void setLoggerShouldSetLogLevel() throws Exception {
this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON) this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON)
.content("{\"configuredLevel\":\"DEBUG\"}")).andExpect(status().isOk()); .content("{\"configuredLevel\":\"DEBUG\"}")).andExpect(status().isOk());
verify(this.loggingSystem).setLogLevel("ROOT", LogLevel.DEBUG); verify(this.loggingSystem).setLogLevel("ROOT", LogLevel.DEBUG);
} }
@Test @Test
public void setLoggerDisabled() throws Exception { public void setLoggerWhenDisabledShouldReturnNotFound() throws Exception {
this.context.getBean(LoggersEndpoint.class).setEnabled(false); this.context.getBean(LoggersEndpoint.class).setEnabled(false);
this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON) this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON)
.content("{\"configuredLevel\":\"DEBUG\"}")) .content("{\"configuredLevel\":\"DEBUG\"}"))
@ -125,11 +137,11 @@ public class LoggersMvcEndpointTests {
verifyZeroInteractions(this.loggingSystem); verifyZeroInteractions(this.loggingSystem);
} }
@Configuration
@Import({ JacksonAutoConfiguration.class, @Import({ JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class }) ManagementServerPropertiesAutoConfiguration.class })
@Configuration
public static class TestConfiguration { public static class TestConfiguration {
@Bean @Bean

@ -16,6 +16,9 @@
package org.springframework.boot.logging; package org.springframework.boot.logging;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -173,4 +176,34 @@ public abstract class AbstractLoggingSystem extends LoggingSystem {
new LoggingSystemProperties(environment).apply(logFile); new LoggingSystemProperties(environment).apply(logFile);
} }
/**
* Maintains a mapping between native levels and {@link LogLevel}.
* @param <T> The native level type
*/
protected static class LogLevels<T> {
private final Map<LogLevel, T> systemToNative;
private final Map<T, LogLevel> nativeToSystem;
public LogLevels() {
this.systemToNative = new HashMap<LogLevel, T>();
this.nativeToSystem = new HashMap<T, LogLevel>();
}
public void map(LogLevel system, T nativeLevel) {
this.systemToNative.put(system, nativeLevel);
this.nativeToSystem.put(nativeLevel, system);
}
public LogLevel convertNativeToSystem(T level) {
return this.nativeToSystem.get(level);
}
public T convertSystemToNative(LogLevel level) {
return this.systemToNative.get(level);
}
}
} }

@ -25,14 +25,14 @@ import org.springframework.util.ObjectUtils;
* @author Ben Hale * @author Ben Hale
* @since 1.5.0 * @since 1.5.0
*/ */
public class LoggerConfiguration { public final class LoggerConfiguration {
private final String name;
private final LogLevel configuredLevel; private final LogLevel configuredLevel;
private final LogLevel effectiveLevel; private final LogLevel effectiveLevel;
private final String name;
/** /**
* Create a new {@link LoggerConfiguration instance}. * Create a new {@link LoggerConfiguration instance}.
* @param name the name of the logger * @param name the name of the logger

@ -45,11 +45,9 @@ public class LoggerConfigurationComparator implements Comparator<LoggerConfigura
if (this.rootLoggerName.equals(o1.getName())) { if (this.rootLoggerName.equals(o1.getName())) {
return -1; return -1;
} }
if (this.rootLoggerName.equals(o2.getName())) { if (this.rootLoggerName.equals(o2.getName())) {
return 1; return 1;
} }
return o1.getName().compareTo(o2.getName()); return o1.getName().compareTo(o2.getName());
} }

@ -16,9 +16,9 @@
package org.springframework.boot.logging; package org.springframework.boot.logging;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -95,31 +95,33 @@ public abstract class LoggingSystem {
} }
/** /**
* Returns the current configuration for a {@link LoggingSystem}'s logger. * Sets the logging level for a given logger.
* @param loggerName the name of the logger * @param loggerName the name of the logger to set
* @return the current configuration * @param level the log level
*/ */
public LoggerConfiguration getLoggerConfiguration(String loggerName) { public void setLogLevel(String loggerName, LogLevel level) {
throw new UnsupportedOperationException( throw new UnsupportedOperationException("Unable to set log level");
"Getting a logger configuration is not supported");
} }
/** /**
* Returns a collection of the current configuration for all a {@link LoggingSystem}'s * Returns a collection of the current configuration for all a {@link LoggingSystem}'s
* loggers. * loggers.
* @return the current configurations * @return the current configurations
* @since 1.5.0
*/ */
public Collection<LoggerConfiguration> listLoggerConfigurations() { public List<LoggerConfiguration> getLoggerConfigurations() {
throw new UnsupportedOperationException( throw new UnsupportedOperationException("Unable to get logger configurations");
"Listing logger configurations is not supported");
} }
/** /**
* Sets the logging level for a given logger. * Returns the current configuration for a {@link LoggingSystem}'s logger.
* @param loggerName the name of the logger to set * @param loggerName the name of the logger
* @param level the log level * @return the current configuration
* @since 1.5.0
*/ */
public abstract void setLogLevel(String loggerName, LogLevel level); public LoggerConfiguration getLoggerConfiguration(String loggerName) {
throw new UnsupportedOperationException("Unable to get logger configuration");
}
/** /**
* Detect and return the logging system in use. Supports Logback and Java Logging. * Detect and return the logging system in use. Supports Logback and Java Logging.
@ -164,18 +166,18 @@ public abstract class LoggingSystem {
} }
@Override @Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) { public void setLogLevel(String loggerName, LogLevel level) {
return null;
} }
@Override @Override
public Collection<LoggerConfiguration> listLoggerConfigurations() { public List<LoggerConfiguration> getLoggerConfigurations() {
return Collections.emptyList(); return Collections.emptyList();
} }
@Override @Override
public void setLogLevel(String loggerName, LogLevel level) { public LoggerConfiguration getLoggerConfiguration(String loggerName) {
return null;
} }
} }

@ -19,12 +19,9 @@ package org.springframework.boot.logging.java;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogManager; import java.util.logging.LogManager;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -51,29 +48,19 @@ import org.springframework.util.StringUtils;
*/ */
public class JavaLoggingSystem extends AbstractLoggingSystem { public class JavaLoggingSystem extends AbstractLoggingSystem {
private static final LoggerConfigurationComparator COMPARATOR = private static final LoggerConfigurationComparator COMPARATOR = new LoggerConfigurationComparator(
new LoggerConfigurationComparator(""); "");
private static final Map<LogLevel, Level> LEVELS; private static final LogLevels<Level> LEVELS = new LogLevels<Level>();
private static final Map<Level, LogLevel> LOG_LEVELS;
static { static {
Map<LogLevel, Level> levels = new HashMap<LogLevel, Level>(); LEVELS.map(LogLevel.TRACE, Level.FINEST);
levels.put(LogLevel.TRACE, Level.FINEST); LEVELS.map(LogLevel.DEBUG, Level.FINE);
levels.put(LogLevel.DEBUG, Level.FINE); LEVELS.map(LogLevel.INFO, Level.INFO);
levels.put(LogLevel.INFO, Level.INFO); LEVELS.map(LogLevel.WARN, Level.WARNING);
levels.put(LogLevel.WARN, Level.WARNING); LEVELS.map(LogLevel.ERROR, Level.SEVERE);
levels.put(LogLevel.ERROR, Level.SEVERE); LEVELS.map(LogLevel.FATAL, Level.SEVERE);
levels.put(LogLevel.FATAL, Level.SEVERE); LEVELS.map(LogLevel.OFF, Level.OFF);
levels.put(LogLevel.OFF, Level.OFF);
LEVELS = Collections.unmodifiableMap(levels);
Map<Level, LogLevel> logLevels = new HashMap<Level, LogLevel>();
for (Map.Entry<LogLevel, Level> entry : LEVELS.entrySet()) {
logLevels.put(entry.getValue(), entry.getKey());
}
LOG_LEVELS = Collections.unmodifiableMap(logLevels);
} }
public JavaLoggingSystem(ClassLoader classLoader) { public JavaLoggingSystem(ClassLoader classLoader) {
@ -127,43 +114,38 @@ public class JavaLoggingSystem extends AbstractLoggingSystem {
} }
@Override @Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) { public void setLogLevel(String loggerName, LogLevel level) {
return toLoggerConfiguration(Logger.getLogger(loggerName)); Assert.notNull(level, "Level must not be null");
String name = (StringUtils.hasText(loggerName) ? loggerName : "");
Logger logger = Logger.getLogger(name);
if (logger != null) {
logger.setLevel(LEVELS.convertSystemToNative(level));
}
} }
@Override @Override
public Collection<LoggerConfiguration> listLoggerConfigurations() { public List<LoggerConfiguration> getLoggerConfigurations() {
List<LoggerConfiguration> result = new ArrayList<LoggerConfiguration>(); List<LoggerConfiguration> result = new ArrayList<LoggerConfiguration>();
for (Enumeration<String> loggerNames = Enumeration<String> names = LogManager.getLogManager().getLoggerNames();
LogManager.getLogManager().getLoggerNames(); while (names.hasMoreElements()) {
loggerNames.hasMoreElements(); ) { result.add(getLoggerConfiguration(names.nextElement()));
result.add(toLoggerConfiguration(Logger.getLogger(
loggerNames.nextElement())));
} }
Collections.sort(result, COMPARATOR); Collections.sort(result, COMPARATOR);
return result; return Collections.unmodifiableList(result);
} }
@Override @Override
public void setLogLevel(String loggerName, LogLevel level) { public LoggerConfiguration getLoggerConfiguration(String loggerName) {
Assert.notNull(level, "Level must not be null"); Logger logger = Logger.getLogger(loggerName);
String name = (StringUtils.hasText(loggerName) ? loggerName : ""); if (logger == null) {
Logger logger = Logger.getLogger(name); return null;
logger.setLevel(LEVELS.get(level));
}
@Override
public Runnable getShutdownHandler() {
return new ShutdownHandler();
} }
LogLevel level = LEVELS.convertNativeToSystem(logger.getLevel());
private static LoggerConfiguration toLoggerConfiguration(Logger logger) { LogLevel effectiveLevel = LEVELS.convertNativeToSystem(getEffectiveLevel(logger));
return new LoggerConfiguration(logger.getName(), return new LoggerConfiguration(logger.getName(), level, effectiveLevel);
LOG_LEVELS.get(logger.getLevel()),
LOG_LEVELS.get(getEffectiveLevel(logger)));
} }
private static Level getEffectiveLevel(Logger root) { private Level getEffectiveLevel(Logger root) {
Logger logger = root; Logger logger = root;
while (logger.getLevel() == null) { while (logger.getLevel() == null) {
logger = logger.getParent(); logger = logger.getParent();
@ -171,6 +153,11 @@ public class JavaLoggingSystem extends AbstractLoggingSystem {
return logger.getLevel(); return logger.getLevel();
} }
@Override
public Runnable getShutdownHandler() {
return new ShutdownHandler();
}
private final class ShutdownHandler implements Runnable { private final class ShutdownHandler implements Runnable {
@Override @Override

@ -20,11 +20,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -33,6 +30,7 @@ import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.config.LoggerConfig;
@ -62,31 +60,21 @@ import org.springframework.util.StringUtils;
*/ */
public class Log4J2LoggingSystem extends Slf4JLoggingSystem { public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
private static final LoggerConfigurationComparator COMPARATOR = private static final LoggerConfigurationComparator COMPARATOR = new LoggerConfigurationComparator(
new LoggerConfigurationComparator(LogManager.ROOT_LOGGER_NAME); LogManager.ROOT_LOGGER_NAME);
private static final String FILE_PROTOCOL = "file"; private static final String FILE_PROTOCOL = "file";
private static final Map<LogLevel, Level> LEVELS; private static final LogLevels<Level> LEVELS = new LogLevels<Level>();
private static final Map<Level, LogLevel> LOG_LEVELS;
static { static {
Map<LogLevel, Level> levels = new HashMap<LogLevel, Level>(); LEVELS.map(LogLevel.TRACE, Level.TRACE);
levels.put(LogLevel.TRACE, Level.TRACE); LEVELS.map(LogLevel.DEBUG, Level.DEBUG);
levels.put(LogLevel.DEBUG, Level.DEBUG); LEVELS.map(LogLevel.INFO, Level.INFO);
levels.put(LogLevel.INFO, Level.INFO); LEVELS.map(LogLevel.WARN, Level.WARN);
levels.put(LogLevel.WARN, Level.WARN); LEVELS.map(LogLevel.ERROR, Level.ERROR);
levels.put(LogLevel.ERROR, Level.ERROR); LEVELS.map(LogLevel.FATAL, Level.FATAL);
levels.put(LogLevel.FATAL, Level.FATAL); LEVELS.map(LogLevel.OFF, Level.OFF);
levels.put(LogLevel.OFF, Level.OFF);
LEVELS = Collections.unmodifiableMap(levels);
Map<Level, LogLevel> logLevels = new HashMap<Level, LogLevel>();
for (Map.Entry<LogLevel, Level> entry : LEVELS.entrySet()) {
logLevels.put(entry.getValue(), entry.getKey());
}
LOG_LEVELS = Collections.unmodifiableMap(logLevels);
} }
private static final Filter FILTER = new AbstractFilter() { private static final Filter FILTER = new AbstractFilter() {
@ -210,33 +198,41 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
} }
@Override @Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) { public void setLogLevel(String loggerName, LogLevel logLevel) {
return toLoggerConfiguration(getLoggerConfig(loggerName)); Level level = LEVELS.convertSystemToNative(logLevel);
LoggerConfig loggerConfig = getLoggerConfig(loggerName);
if (loggerConfig == null) {
loggerConfig = new LoggerConfig(loggerName, level, true);
getLoggerContext().getConfiguration().addLogger(loggerName, loggerConfig);
}
else {
loggerConfig.setLevel(level);
}
getLoggerContext().updateLoggers();
} }
@Override @Override
public Collection<LoggerConfiguration> listLoggerConfigurations() { public List<LoggerConfiguration> getLoggerConfigurations() {
List<LoggerConfiguration> result = new ArrayList<LoggerConfiguration>(); List<LoggerConfiguration> result = new ArrayList<LoggerConfiguration>();
for (LoggerConfig loggerConfig : Configuration configuration = getLoggerContext().getConfiguration();
getLoggerContext().getConfiguration().getLoggers().values()) { for (LoggerConfig loggerConfig : configuration.getLoggers().values()) {
result.add(toLoggerConfiguration(loggerConfig)); result.add(convertLoggerConfiguration(loggerConfig));
} }
Collections.sort(result, COMPARATOR); Collections.sort(result, COMPARATOR);
return result; return result;
} }
@Override @Override
public void setLogLevel(String loggerName, LogLevel logLevel) { public LoggerConfiguration getLoggerConfiguration(String loggerName) {
Level level = LEVELS.get(logLevel); return convertLoggerConfiguration(getLoggerConfig(loggerName));
LoggerConfig loggerConfig = getLoggerConfig(loggerName);
if (loggerConfig == null) {
loggerConfig = new LoggerConfig(loggerName, level, true);
getLoggerContext().getConfiguration().addLogger(loggerName, loggerConfig);
} }
else {
loggerConfig.setLevel(level); private LoggerConfiguration convertLoggerConfiguration(LoggerConfig loggerConfig) {
if (loggerConfig == null) {
return null;
} }
getLoggerContext().updateLoggers(); LogLevel level = LEVELS.convertNativeToSystem(loggerConfig.getLevel());
return new LoggerConfiguration(loggerConfig.getName(), level, level);
} }
@Override @Override
@ -272,12 +268,6 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
loggerContext.setExternalContext(null); loggerContext.setExternalContext(null);
} }
private static LoggerConfiguration toLoggerConfiguration(LoggerConfig loggerConfig) {
return new LoggerConfiguration(loggerConfig.getName(),
LOG_LEVELS.get(loggerConfig.getLevel()),
LOG_LEVELS.get(loggerConfig.getLevel()));
}
private final class ShutdownHandler implements Runnable { private final class ShutdownHandler implements Runnable {
@Override @Override

@ -20,11 +20,8 @@ import java.net.URL;
import java.security.CodeSource; import java.security.CodeSource;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.LoggerContext;
@ -61,31 +58,21 @@ import org.springframework.util.StringUtils;
*/ */
public class LogbackLoggingSystem extends Slf4JLoggingSystem { public class LogbackLoggingSystem extends Slf4JLoggingSystem {
private static final LoggerConfigurationComparator COMPARATOR = private static final LoggerConfigurationComparator COMPARATOR = new LoggerConfigurationComparator(
new LoggerConfigurationComparator(Logger.ROOT_LOGGER_NAME); Logger.ROOT_LOGGER_NAME);
private static final String CONFIGURATION_FILE_PROPERTY = "logback.configurationFile"; private static final String CONFIGURATION_FILE_PROPERTY = "logback.configurationFile";
private static final Map<LogLevel, Level> LEVELS; private static final LogLevels<Level> LEVELS = new LogLevels<Level>();
private static final Map<Level, LogLevel> LOG_LEVELS;
static { static {
Map<LogLevel, Level> levels = new HashMap<LogLevel, Level>(); LEVELS.map(LogLevel.TRACE, Level.TRACE);
levels.put(LogLevel.TRACE, Level.TRACE); LEVELS.map(LogLevel.DEBUG, Level.DEBUG);
levels.put(LogLevel.DEBUG, Level.DEBUG); LEVELS.map(LogLevel.INFO, Level.INFO);
levels.put(LogLevel.INFO, Level.INFO); LEVELS.map(LogLevel.WARN, Level.WARN);
levels.put(LogLevel.WARN, Level.WARN); LEVELS.map(LogLevel.ERROR, Level.ERROR);
levels.put(LogLevel.ERROR, Level.ERROR); LEVELS.map(LogLevel.FATAL, Level.ERROR);
levels.put(LogLevel.FATAL, Level.ERROR); LEVELS.map(LogLevel.OFF, Level.OFF);
levels.put(LogLevel.OFF, Level.OFF);
LEVELS = Collections.unmodifiableMap(levels);
Map<Level, LogLevel> logLevels = new HashMap<Level, LogLevel>();
for (Map.Entry<LogLevel, Level> entry : LEVELS.entrySet()) {
logLevels.put(entry.getValue(), entry.getKey());
}
LOG_LEVELS = Collections.unmodifiableMap(logLevels);
} }
private static final TurboFilter FILTER = new TurboFilter() { private static final TurboFilter FILTER = new TurboFilter() {
@ -226,23 +213,37 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
} }
@Override @Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) { public List<LoggerConfiguration> getLoggerConfigurations() {
return toLoggerConfiguration(getLogger(loggerName));
}
@Override
public Collection<LoggerConfiguration> listLoggerConfigurations() {
List<LoggerConfiguration> result = new ArrayList<LoggerConfiguration>(); List<LoggerConfiguration> result = new ArrayList<LoggerConfiguration>();
for (ch.qos.logback.classic.Logger logger : getLoggerContext().getLoggerList()) { for (ch.qos.logback.classic.Logger logger : getLoggerContext().getLoggerList()) {
result.add(toLoggerConfiguration(logger)); result.add(getLoggerConfiguration(logger));
} }
Collections.sort(result, COMPARATOR); Collections.sort(result, COMPARATOR);
return result; return result;
} }
@Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) {
return getLoggerConfiguration(getLogger(loggerName));
}
private LoggerConfiguration getLoggerConfiguration(
ch.qos.logback.classic.Logger logger) {
if (logger == null) {
return null;
}
LogLevel level = LEVELS.convertNativeToSystem(logger.getLevel());
LogLevel effectiveLevel = LEVELS
.convertNativeToSystem(logger.getEffectiveLevel());
return new LoggerConfiguration(logger.getName(), level, effectiveLevel);
}
@Override @Override
public void setLogLevel(String loggerName, LogLevel level) { public void setLogLevel(String loggerName, LogLevel level) {
getLogger(loggerName).setLevel(LEVELS.get(level)); ch.qos.logback.classic.Logger logger = getLogger(loggerName);
if (logger != null) {
logger.setLevel(LEVELS.convertSystemToNative(level));
}
} }
@Override @Override
@ -252,8 +253,8 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
private ch.qos.logback.classic.Logger getLogger(String name) { private ch.qos.logback.classic.Logger getLogger(String name) {
LoggerContext factory = getLoggerContext(); LoggerContext factory = getLoggerContext();
return factory name = (StringUtils.isEmpty(name) ? Logger.ROOT_LOGGER_NAME : name);
.getLogger(StringUtils.isEmpty(name) ? Logger.ROOT_LOGGER_NAME : name); return factory.getLogger(name);
} }
@ -296,13 +297,6 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
loggerContext.removeObject(LoggingSystem.class.getName()); loggerContext.removeObject(LoggingSystem.class.getName());
} }
private static LoggerConfiguration toLoggerConfiguration(
ch.qos.logback.classic.Logger logger) {
return new LoggerConfiguration(logger.getName(),
LOG_LEVELS.get(logger.getLevel()),
LOG_LEVELS.get(logger.getEffectiveLevel()));
}
private final class ShutdownHandler implements Runnable { private final class ShutdownHandler implements Runnable {
@Override @Override

@ -27,8 +27,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class LoggerConfigurationComparatorTests { public class LoggerConfigurationComparatorTests {
private final LoggerConfigurationComparator comparator = private final LoggerConfigurationComparator comparator = new LoggerConfigurationComparator(
new LoggerConfigurationComparator("ROOT"); "ROOT");
@Test @Test
public void rootLoggerFirst() { public void rootLoggerFirst() {

@ -18,7 +18,7 @@ package org.springframework.boot.logging;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Handler; import java.util.logging.Handler;
@ -519,18 +519,17 @@ public class LoggingApplicationListenerTests {
} }
@Override @Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) { public void setLogLevel(String loggerName, LogLevel level) {
return null;
} }
@Override @Override
public Collection<LoggerConfiguration> listLoggerConfigurations() { public List<LoggerConfiguration> getLoggerConfigurations() {
return null; return null;
} }
@Override @Override
public void setLogLevel(String loggerName, LogLevel level) { public LoggerConfiguration getLoggerConfiguration(String loggerName) {
return null;
} }
@Override @Override
@ -564,27 +563,24 @@ public class LoggingApplicationListenerTests {
private boolean cleanedUp = false; private boolean cleanedUp = false;
public TestCleanupLoggingSystem(ClassLoader classLoader) { public TestCleanupLoggingSystem(ClassLoader classLoader) {
} }
@Override @Override
public void beforeInitialize() { public void beforeInitialize() {
} }
@Override @Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) { public void setLogLevel(String loggerName, LogLevel level) {
return null;
} }
@Override @Override
public Collection<LoggerConfiguration> listLoggerConfigurations() { public List<LoggerConfiguration> getLoggerConfigurations() {
return null; return null;
} }
@Override @Override
public void setLogLevel(String loggerName, LogLevel level) { public LoggerConfiguration getLoggerConfiguration(String loggerName) {
return null;
} }
@Override @Override

@ -49,7 +49,7 @@ public class LoggingSystemTests {
@Test(expected = UnsupportedOperationException.class) @Test(expected = UnsupportedOperationException.class)
public void listLoggerConfigurationsIsUnsupported() { public void listLoggerConfigurationsIsUnsupported() {
new StubLoggingSystem().listLoggerConfigurations(); new StubLoggingSystem().getLoggerConfigurations();
} }
private static final class StubLoggingSystem extends LoggingSystem { private static final class StubLoggingSystem extends LoggingSystem {

@ -19,7 +19,7 @@ package org.springframework.boot.logging.java;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.logging.Level; import java.util.logging.Level;
@ -149,35 +149,36 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void getLoggingConfiguration() throws Exception { public void setLevel() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null); this.loggingSystem.initialize(null, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG); this.logger.debug("Hello");
assertThat(this.loggingSystem.getLoggerConfiguration(getClass().getName())) this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
.isEqualTo(new LoggerConfiguration(getClass().getName(), this.logger.debug("Hello");
LogLevel.DEBUG, LogLevel.DEBUG)); assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello"))
.isEqualTo(1);
} }
@Test @Test
public void listLoggingConfigurations() throws Exception { public void getLoggingConfigurations() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null); this.loggingSystem.initialize(null, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG); this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
Collection<LoggerConfiguration> loggerConfigurations = this.loggingSystem List<LoggerConfiguration> configurations = this.loggingSystem
.listLoggerConfigurations(); .getLoggerConfigurations();
assertThat(loggerConfigurations.size()).isGreaterThan(0); assertThat(configurations).isNotEmpty();
assertThat(loggerConfigurations.iterator().next().getName()).isEmpty(); assertThat(configurations.get(0).getName()).isEmpty();
} }
@Test @Test
public void setLevel() throws Exception { public void getLoggingConfiguration() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null); this.loggingSystem.initialize(null, null, null);
this.logger.debug("Hello"); this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG); LoggerConfiguration configuration = this.loggingSystem
this.logger.debug("Hello"); .getLoggerConfiguration(getClass().getName());
assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello")) assertThat(configuration).isEqualTo(new LoggerConfiguration(getClass().getName(),
.isEqualTo(1); LogLevel.DEBUG, LogLevel.DEBUG));
} }
} }

@ -21,7 +21,6 @@ import java.beans.PropertyChangeListener;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -124,35 +123,36 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void getLoggingConfiguration() throws Exception { public void setLevel() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null); this.loggingSystem.initialize(null, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG); this.logger.debug("Hello");
assertThat(this.loggingSystem.getLoggerConfiguration(getClass().getName())) this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
.isEqualTo(new LoggerConfiguration(getClass().getName(), this.logger.debug("Hello");
LogLevel.DEBUG, LogLevel.DEBUG)); assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello"))
.isEqualTo(1);
} }
@Test @Test
public void listLoggingConfigurations() throws Exception { public void getLoggingConfigurations() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null); this.loggingSystem.initialize(null, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG); this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
Collection<LoggerConfiguration> loggerConfigurations = this.loggingSystem List<LoggerConfiguration> configurations = this.loggingSystem
.listLoggerConfigurations(); .getLoggerConfigurations();
assertThat(loggerConfigurations.size()).isGreaterThan(0); assertThat(configurations).isNotEmpty();
assertThat(loggerConfigurations.iterator().next().getName()).isEmpty(); assertThat(configurations.get(0).getName()).isEmpty();
} }
@Test @Test
public void setLevel() throws Exception { public void getLoggingConfiguration() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null); this.loggingSystem.initialize(null, null, null);
this.logger.debug("Hello"); this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG); LoggerConfiguration configuration = this.loggingSystem
this.logger.debug("Hello"); .getLoggerConfiguration(getClass().getName());
assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello")) assertThat(configuration).isEqualTo(new LoggerConfiguration(getClass().getName(),
.isEqualTo(1); LogLevel.DEBUG, LogLevel.DEBUG));
} }
@Test @Test

@ -18,7 +18,7 @@ package org.springframework.boot.logging.logback;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.util.Collection; import java.util.List;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.LogManager; import java.util.logging.LogManager;
@ -163,36 +163,37 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void getLoggingConfiguration() throws Exception { public void setLevel() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null); this.loggingSystem.initialize(this.initializationContext, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG); this.logger.debug("Hello");
assertThat(this.loggingSystem.getLoggerConfiguration(getClass().getName())) this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
.isEqualTo(new LoggerConfiguration(getClass().getName(), this.logger.debug("Hello");
LogLevel.DEBUG, LogLevel.DEBUG)); assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello"))
.isEqualTo(1);
} }
@Test @Test
public void listLoggingConfigurations() throws Exception { public void getLoggingConfigurations() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null); this.loggingSystem.initialize(this.initializationContext, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG); this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
Collection<LoggerConfiguration> loggerConfigurations = this.loggingSystem List<LoggerConfiguration> configurations = this.loggingSystem
.listLoggerConfigurations(); .getLoggerConfigurations();
assertThat(loggerConfigurations.size()).isGreaterThan(0); assertThat(configurations).isNotEmpty();
assertThat(loggerConfigurations.iterator().next().getName()).isEqualTo( assertThat(configurations.get(0).getName())
org.slf4j.Logger.ROOT_LOGGER_NAME); .isEqualTo(org.slf4j.Logger.ROOT_LOGGER_NAME);
} }
@Test @Test
public void setLevel() throws Exception { public void getLoggingConfiguration() throws Exception {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null); this.loggingSystem.initialize(this.initializationContext, null, null);
this.logger.debug("Hello"); this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG); LoggerConfiguration configuration = this.loggingSystem
this.logger.debug("Hello"); .getLoggerConfiguration(getClass().getName());
assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello")) assertThat(configuration).isEqualTo(new LoggerConfiguration(getClass().getName(),
.isEqualTo(1); LogLevel.DEBUG, LogLevel.DEBUG));
} }
@Test @Test

Loading…
Cancel
Save