Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'logging-configurator' of stuartgunter/dropwizard
- Loading branch information
Showing
5 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
dropwizard-logging/src/main/java/io/dropwizard/logging/tasks/LogConfigurationTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.dropwizard.logging.tasks; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import com.google.common.collect.ImmutableMultimap; | ||
import io.dropwizard.servlets.tasks.Task; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.List; | ||
|
||
/** | ||
* <p>Sets the logging level for a number of loggers</p> | ||
* | ||
* <b>Parameters:</b> | ||
* <table> | ||
* <tr> | ||
* <td>Name</td> | ||
* <td>Description</td> | ||
* </tr> | ||
* <tr> | ||
* <td>logger</td> | ||
* <td>One or more logger names to be configured with the specified log level.</td> | ||
* </tr> | ||
* <tr> | ||
* <td>level</td> | ||
* <td>An optional Logback {@link Level} to configure. If not provided, the log level will be set to null.</td> | ||
* </tr> | ||
* </table> | ||
*/ | ||
public class LogConfigurationTask extends Task { | ||
|
||
private final LoggerContext loggerContext; | ||
|
||
/** | ||
* Creates a new LogConfigurationTask. | ||
*/ | ||
public LogConfigurationTask() { | ||
this((LoggerContext) LoggerFactory.getILoggerFactory()); | ||
} | ||
|
||
/** | ||
* Creates a new LogConfigurationTask with the given {@link ch.qos.logback.classic.LoggerContext} instance. | ||
* <p/> | ||
* <b>Use {@link LogConfigurationTask#LogConfigurationTask()} instead.</b> | ||
* | ||
* @param loggerContext a {@link ch.qos.logback.classic.LoggerContext} instance | ||
*/ | ||
public LogConfigurationTask(LoggerContext loggerContext) { | ||
super("log-level"); | ||
this.loggerContext = loggerContext; | ||
} | ||
|
||
@Override | ||
public void execute(ImmutableMultimap<String, String> parameters, PrintWriter output) throws Exception { | ||
List<String> loggerNames = getLoggerNames(parameters); | ||
Level loggerLevel = getLoggerLevel(parameters); | ||
|
||
for (String loggerName : loggerNames) { | ||
loggerContext.getLogger(loggerName).setLevel(loggerLevel); | ||
output.println(String.format("Configured logging level for %s to %s", loggerName, loggerLevel)); | ||
output.flush(); | ||
} | ||
} | ||
|
||
private List<String> getLoggerNames(ImmutableMultimap<String, String> parameters) { | ||
return parameters.get("logger").asList(); | ||
} | ||
|
||
private Level getLoggerLevel(ImmutableMultimap<String, String> parameters) { | ||
List<String> loggerLevels = parameters.get("level").asList(); | ||
return loggerLevels.isEmpty() ? null : Level.valueOf(loggerLevels.get(0)); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
dropwizard-logging/src/test/java/io/dropwizard/logging/tasks/LogConfigurationTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.dropwizard.logging.tasks; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import com.google.common.collect.ImmutableMultimap; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class LogConfigurationTaskTest { | ||
|
||
private static final Level DEFAULT_LEVEL = Level.ALL; | ||
|
||
private final LoggerContext loggerContext = new LoggerContext(); | ||
private final Logger logger1 = loggerContext.getLogger("logger.one"); | ||
private final Logger logger2 = loggerContext.getLogger("logger.two"); | ||
|
||
private final StringWriter stringWriter = new StringWriter(); | ||
private final PrintWriter output = new PrintWriter(stringWriter); | ||
|
||
private final LogConfigurationTask task = new LogConfigurationTask(loggerContext); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
logger1.setLevel(DEFAULT_LEVEL); | ||
logger2.setLevel(DEFAULT_LEVEL); | ||
} | ||
|
||
@Test | ||
public void configuresSpecificLevelForALogger() throws Exception { | ||
// given | ||
ImmutableMultimap<String, String> parameters = ImmutableMultimap.of( | ||
"logger", "logger.one", | ||
"level", "debug"); | ||
|
||
// when | ||
task.execute(parameters, output); | ||
|
||
// then | ||
assertThat(logger1.getLevel()).isEqualTo(Level.DEBUG); | ||
assertThat(logger2.getLevel()).isEqualTo(DEFAULT_LEVEL); | ||
|
||
assertThat(stringWriter.toString()).isEqualTo("Configured logging level for logger.one to DEBUG\n"); | ||
} | ||
|
||
@Test | ||
public void configuresDefaultLevelForALogger() throws Exception { | ||
// given | ||
ImmutableMultimap<String, String> parameters = ImmutableMultimap.of( | ||
"logger", "logger.one"); | ||
|
||
// when | ||
task.execute(parameters, output); | ||
|
||
// then | ||
assertThat(logger1.getLevel()).isNull(); | ||
assertThat(logger2.getLevel()).isEqualTo(DEFAULT_LEVEL); | ||
|
||
assertThat(stringWriter.toString()).isEqualTo("Configured logging level for logger.one to null\n"); | ||
} | ||
|
||
@Test | ||
public void configuresLevelForMultipleLoggers() throws Exception { | ||
// given | ||
ImmutableMultimap<String, String> parameters = ImmutableMultimap.of( | ||
"logger", "logger.one", | ||
"logger", "logger.two", | ||
"level", "INFO"); | ||
|
||
// when | ||
task.execute(parameters, output); | ||
|
||
// then | ||
assertThat(logger1.getLevel()).isEqualTo(Level.INFO); | ||
assertThat(logger2.getLevel()).isEqualTo(Level.INFO); | ||
|
||
assertThat(stringWriter.toString()).isEqualTo( | ||
"Configured logging level for logger.one to INFO\n" + | ||
"Configured logging level for logger.two to INFO\n"); | ||
} | ||
} |