From 921588a2447d2dc932e1165f6326150b365d56da Mon Sep 17 00:00:00 2001 From: Artem Prigoda Date: Thu, 8 Feb 2018 13:31:46 +0100 Subject: [PATCH] Add support for providing a custom layout during logging bootstrap 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. --- .../logging/json/LayoutIntegrationTests.java | 3 ++- .../dropwizard/logging/BootstrapLogging.java | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/dropwizard-json-logging/src/test/java/io/dropwizard/logging/json/LayoutIntegrationTests.java b/dropwizard-json-logging/src/test/java/io/dropwizard/logging/json/LayoutIntegrationTests.java index 9a17d81885b..5ccd14505ee 100644 --- a/dropwizard-json-logging/src/test/java/io/dropwizard/logging/json/LayoutIntegrationTests.java +++ b/dropwizard-json-logging/src/test/java/io/dropwizard/logging/json/LayoutIntegrationTests.java @@ -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(); diff --git a/dropwizard-logging/src/main/java/io/dropwizard/logging/BootstrapLogging.java b/dropwizard-logging/src/main/java/io/dropwizard/logging/BootstrapLogging.java index a6ed7a6cd1f..fe1c079b218 100644 --- a/dropwizard-logging/src/main/java/io/dropwizard/logging/BootstrapLogging.java +++ b/dropwizard-logging/src/main/java/io/dropwizard/logging/BootstrapLogging.java @@ -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; @@ -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(); @@ -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 layout = layoutFactory.create(root.getLoggerContext(), TimeZone.getDefault()); + layout.start(); final ThresholdFilter filter = new ThresholdFilter(); filter.setLevel(level.toString()); @@ -57,7 +63,7 @@ public static void bootstrap(Level level) { appender.setContext(root.getLoggerContext()); final LayoutWrappingEncoder layoutEncoder = new LayoutWrappingEncoder<>(); - layoutEncoder.setLayout(formatter); + layoutEncoder.setLayout(layout); appender.setEncoder(layoutEncoder); appender.start(); @@ -67,4 +73,10 @@ public static void bootstrap(Level level) { BOOTSTRAPPING_LOCK.unlock(); } } + + @FunctionalInterface + public interface LayoutFactory { + + Layout create(LoggerContext loggerContext, TimeZone timeZone); + } }