Skip to content

Commit

Permalink
Support Dropwizard applications without logback
Browse files Browse the repository at this point in the history
  • Loading branch information
arteam committed Jan 24, 2017
1 parent ec08450 commit 0b1d9e3
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 23 deletions.
8 changes: 6 additions & 2 deletions dropwizard-core/src/main/java/io/dropwizard/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
*/
public abstract class Application<T extends Configuration> {
protected Application() {
// make sure spinning up Hibernate Validator doesn't yell at us
BootstrapLogging.bootstrap(bootstrapLogLevel());
bootstrapLogging();
}

/**
Expand All @@ -34,6 +33,11 @@ protected Level bootstrapLogLevel() {
return Level.WARN;
}

protected void bootstrapLogging() {
// make sure spinning up Hibernate Validator doesn't yell at us
BootstrapLogging.bootstrap(bootstrapLogLevel());
}

/**
* Returns the {@link Class} of the configuration class type parameter.
*
Expand Down
19 changes: 11 additions & 8 deletions dropwizard-core/src/main/java/io/dropwizard/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public class Configuration {
private ServerFactory server = new DefaultServerFactory();

@Valid
@NotNull
private LoggingFactory logging = new DefaultLoggingFactory();
private LoggingFactory logging;

@Valid
@NotNull
Expand Down Expand Up @@ -96,15 +95,19 @@ public void setServerFactory(ServerFactory factory) {
* @return logging-specific configuration parameters
*/
@JsonProperty("logging")
public LoggingFactory getLoggingFactory() {
public synchronized LoggingFactory getLoggingFactory() {
if (logging == null) {
// Lazy init to avoid a hard dependency to logback
logging = new DefaultLoggingFactory();
}
return logging;
}

/**
* Sets the logging-specific section of the configuration file.
*/
@JsonProperty("logging")
public void setLoggingFactory(LoggingFactory factory) {
public synchronized void setLoggingFactory(LoggingFactory factory) {
this.logging = factory;
}

Expand All @@ -121,9 +124,9 @@ public void setMetricsFactory(MetricsFactory metrics) {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("server", server)
.add("logging", logging)
.add("metrics", metrics)
.toString();
.add("server", server)
.add("logging", logging)
.add("metrics", metrics)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ public abstract class AbstractServerFactory implements ServerFactory {
private static final Pattern WINDOWS_NEWLINE = Pattern.compile("\\r\\n?");

@Valid
@NotNull
private RequestLogFactory requestLog = new LogbackAccessRequestLogFactory();
private RequestLogFactory requestLog;

@Valid
@NotNull
Expand Down Expand Up @@ -263,12 +262,16 @@ public boolean isThreadPoolSizedCorrectly() {
}

@JsonProperty("requestLog")
public RequestLogFactory getRequestLogFactory() {
public synchronized RequestLogFactory getRequestLogFactory() {
if (requestLog == null) {
// Lazy init to avoid a hard dependency to logback
requestLog = new LogbackAccessRequestLogFactory();
}
return requestLog;
}

@JsonProperty("requestLog")
public void setRequestLogFactory(RequestLogFactory requestLog) {
public synchronized void setRequestLogFactory(RequestLogFactory requestLog) {
this.requestLog = requestLog;
}

Expand Down Expand Up @@ -576,9 +579,9 @@ protected SetUIDListener buildSetUIDListener() {
}

protected Handler addRequestLog(Server server, Handler handler, String name) {
if (requestLog.isEnabled()) {
if (getRequestLogFactory().isEnabled()) {
final RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(requestLog.build(name));
requestLogHandler.setRequestLog(getRequestLogFactory().build(name));
// server should own the request log's lifecycle since it's already started,
// the handler might not become managed in case of an error which would leave
// the request log stranded
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.dropwizard.logging;

import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonTypeName;

/**
* A logging factory which does configure a logging infrastructure.
* Useful when the users doesn't want to use the Dropwizard logging configuration abilities.
*/
@JsonTypeName("external")
public class ExternalLoggingFactory implements LoggingFactory {

@Override
public void configure(MetricRegistry metricRegistry, String name) {

}

@Override
public void stop() {

}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.dropwizard.logging.DefaultLoggingFactory
io.dropwizard.logging.ExternalLoggingFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.dropwizard.request.logging;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Slf4jRequestLog;

/**
* A request log factory which writes request logs via Slf4j and doesn't configure
* any logging infrastructure.
* Useful when the user doesn't want to configure request logging via Dropwizard configuration.
*/
@JsonTypeName("external")
public class ExternalRequestLogFactory implements RequestLogFactory {

private boolean enabled = true;

@JsonProperty
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

@Override
@JsonProperty
public boolean isEnabled() {
return enabled;
}

@Override
public RequestLog build(String name) {
return new Slf4jRequestLog();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
io.dropwizard.request.logging.LogbackAccessRequestLogFactory
io.dropwizard.request.logging.old.LogbackClassicRequestLogFactory
io.dropwizard.request.logging.ExternalRequestLogFactory
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.google.common.collect.ImmutableMultimap;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
Expand Down Expand Up @@ -30,34 +31,33 @@
*/
public class LogConfigurationTask extends Task {

private final LoggerContext loggerContext;
private final ILoggerFactory loggerContext;

/**
* Creates a new LogConfigurationTask.
*/
public LogConfigurationTask() {
this((LoggerContext) LoggerFactory.getILoggerFactory());
this(LoggerFactory.getILoggerFactory());
}

/**
* Creates a new LogConfigurationTask with the given {@link ch.qos.logback.classic.LoggerContext} instance.
* Creates a new LogConfigurationTask with the given {@link ILoggerFactory} instance.
* <p/>
* <b>Use {@link LogConfigurationTask#LogConfigurationTask()} instead.</b>
*
* @param loggerContext a {@link ch.qos.logback.classic.LoggerContext} instance
* @param loggerContext a {@link ILoggerFactory} instance
*/
public LogConfigurationTask(LoggerContext loggerContext) {
public LogConfigurationTask(ILoggerFactory loggerContext) {
super("log-level");
this.loggerContext = loggerContext;
}

@Override
public void execute(ImmutableMultimap<String, String> parameters, PrintWriter output) throws Exception {
final List<String> loggerNames = getLoggerNames(parameters);
final Level loggerLevel = getLoggerLevel(parameters);

for (String loggerName : loggerNames) {
loggerContext.getLogger(loggerName).setLevel(loggerLevel);
((LoggerContext) loggerContext).getLogger(loggerName).setLevel(loggerLevel);
output.println(String.format("Configured logging level for %s to %s", loggerName, loggerLevel));
output.flush();
}
Expand Down

0 comments on commit 0b1d9e3

Please sign in to comment.