Skip to content

Commit

Permalink
Merge pull request #2260 from dropwizard/boostrap-layout-factory
Browse files Browse the repository at this point in the history
Add support for providing a custom layout during logging bootstrap
  • Loading branch information
jplock committed Feb 8, 2018
2 parents a4da953 + b3beebd commit 3f9cf22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Expand Up @@ -40,7 +40,7 @@
public class LayoutIntegrationTests {

static {
BootstrapLogging.bootstrap(Level.INFO);
BootstrapLogging.bootstrap(Level.INFO, new EventJsonLayoutBaseFactory());
}

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

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 io.dropwizard.logging.layout.DiscoverableLayoutFactory;

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

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

public static void bootstrap(Level level, DiscoverableLayoutFactory<ILoggingEvent> layoutFactory) {
LoggingUtil.hijackJDKLogging();

BOOTSTRAPPING_LOCK.lock();
Expand All @@ -45,8 +52,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.build(root.getLoggerContext(), TimeZone.getDefault());
layout.start();

final ThresholdFilter filter = new ThresholdFilter();
filter.setLevel(level.toString());
Expand All @@ -57,7 +64,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 Down

0 comments on commit 3f9cf22

Please sign in to comment.