Skip to content

Commit

Permalink
Add support for providing a custom layout during logging bootstrap
Browse files Browse the repository at this point in the history
Dropwizard users have the ability to configure their application to
use JSON as the logging format. Unfortunately, the default logging
bootstrap mechanism which kicks off before Dropwizard logging
configuration, uses the default logging layout. As a result, some
internal logging messages (Jetty, Hibernate initialization) are
produced in a not recognized format.

This change add possibility to specify a layout factory during
the bootstrap process. Users can override the bootstrap method of
`Application` and provide an own layout format. For example,

```
BootstrapLogging.bootstrap(Level.INFO, (loggerContext, timeZone) ->
    new EventJsonLayoutBaseFactory().build(loggerContext, timeZone));
```

As a result, the all applications logs will produced in the JSON format.
  • Loading branch information
arteam committed Feb 8, 2018
1 parent 51c3690 commit 921588a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Expand Up @@ -40,7 +40,8 @@
public class LayoutIntegrationTests {

static {
BootstrapLogging.bootstrap(Level.INFO);
BootstrapLogging.bootstrap(Level.INFO, (loggerContext, timeZone) ->
new EventJsonLayoutBaseFactory().build(loggerContext, timeZone));
}

private final ObjectMapper objectMapper = Jackson.newObjectMapper();
Expand Down
Expand Up @@ -2,9 +2,11 @@

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;

import javax.annotation.concurrent.GuardedBy;
Expand Down Expand Up @@ -35,6 +37,10 @@ public static void bootstrap() {
}

public static void bootstrap(Level level) {
bootstrap(level, DropwizardLayout::new);
}

public static void bootstrap(Level level, LayoutFactory layoutFactory) {
LoggingUtil.hijackJDKLogging();

BOOTSTRAPPING_LOCK.lock();
Expand All @@ -45,8 +51,8 @@ public static void bootstrap(Level level) {
final Logger root = LoggingUtil.getLoggerContext().getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
root.detachAndStopAllAppenders();

final DropwizardLayout formatter = new DropwizardLayout(root.getLoggerContext(), TimeZone.getDefault());
formatter.start();
final Layout<ILoggingEvent> layout = layoutFactory.create(root.getLoggerContext(), TimeZone.getDefault());
layout.start();

final ThresholdFilter filter = new ThresholdFilter();
filter.setLevel(level.toString());
Expand All @@ -57,7 +63,7 @@ public static void bootstrap(Level level) {
appender.setContext(root.getLoggerContext());

final LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
layoutEncoder.setLayout(formatter);
layoutEncoder.setLayout(layout);
appender.setEncoder(layoutEncoder);
appender.start();

Expand All @@ -67,4 +73,10 @@ public static void bootstrap(Level level) {
BOOTSTRAPPING_LOCK.unlock();
}
}

@FunctionalInterface
public interface LayoutFactory {

Layout<ILoggingEvent> create(LoggerContext loggerContext, TimeZone timeZone);
}
}

0 comments on commit 921588a

Please sign in to comment.