Last active
May 15, 2020 14:17
-
-
Save LukasForst/ebcc49b9517d16c3821c1c1e692690bf to your computer and use it in GitHub Desktop.
Logging to custom defined json with dropwizard
This file contains hidden or 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
| // | |
| // Wire | |
| // Copyright (C) 2016 Wire Swiss GmbH | |
| // | |
| // This program is free software: you can redistribute it and/or modify | |
| // it under the terms of the GNU General Public License as published by | |
| // the Free Software Foundation, either version 3 of the License, or | |
| // (at your option) any later version. | |
| // | |
| // This program is distributed in the hope that it will be useful, | |
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| // GNU General Public License for more details. | |
| // | |
| // You should have received a copy of the GNU General Public License | |
| // along with this program. If not, see http://www.gnu.org/licenses/. | |
| // | |
| package com.wire.bots.echo; | |
| import com.wire.bots.sdk.Configuration; | |
| import io.dropwizard.logging.LoggingFactory; | |
| public class Config extends Configuration { | |
| public String ingress; | |
| public int portMin; | |
| public int portMax; | |
| public String module; | |
| public String getIngress() { | |
| return ingress; | |
| } | |
| public int getPortMin() { | |
| return portMin; | |
| } | |
| public int getPortMax() { | |
| return portMax; | |
| } | |
| public String getModule() { | |
| return module; | |
| } | |
| @Override | |
| public synchronized LoggingFactory getLoggingFactory() { | |
| return new LogbackAutoConfigLoggingFactory(); | |
| } | |
| } |
This file contains hidden or 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
| package com.wire.bots.echo; | |
| import ch.qos.logback.classic.spi.ILoggingEvent; | |
| import ch.qos.logback.core.CoreConstants; | |
| import ch.qos.logback.core.LayoutBase; | |
| import java.time.Instant; | |
| import java.time.ZoneOffset; | |
| import java.time.format.DateTimeFormatter; | |
| import java.util.Map; | |
| public class CustomLoggingLayout extends LayoutBase<ILoggingEvent> { | |
| private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC); | |
| @Override | |
| public String doLayout(ILoggingEvent event) { | |
| StringBuffer buffer = new StringBuffer(256); | |
| buffer.append("{"); | |
| appendJson(buffer, "@timestamp", formatTime(event)); | |
| Map<String, String> mdc = event.getMDCPropertyMap(); | |
| if (mdc.containsKey("infra_request")) { | |
| appendJson(buffer, "infra_request", mdc.get("infra_request")); | |
| } | |
| if (mdc.containsKey("app_request")) { | |
| appendJson(buffer, "app_request", mdc.get("app_request")); | |
| } | |
| appendJson(buffer, "logger", event.getLoggerName()); | |
| appendJson(buffer, "message", event.getFormattedMessage()); | |
| appendJson(buffer, "level", event.getLevel().levelStr); | |
| appendJson(buffer, "thread_name", event.getThreadName(), ""); | |
| buffer.append("}") | |
| .append(CoreConstants.LINE_SEPARATOR); | |
| return buffer.toString(); | |
| } | |
| private void appendJson(StringBuffer buffer, String key, String value) { | |
| appendJson(buffer, key, value, ","); | |
| } | |
| private void appendJson(StringBuffer buffer, String key, String value, String ending) { | |
| buffer.append("\"") | |
| .append(key) | |
| .append("\":\"") | |
| .append(value) | |
| .append("\"") | |
| .append(ending); | |
| } | |
| private String formatTime(ILoggingEvent event) { | |
| return dateTimeFormatter.format(Instant.ofEpochMilli(event.getTimeStamp())); | |
| } | |
| } |
This file contains hidden or 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
| <configuration> | |
| <appender name="local" class="ch.qos.logback.core.ConsoleAppender"> | |
| <encoder> | |
| <pattern>%d{dd/MM/yyyy HH:mm:ss} [%level] [%X{mdc-val}] %logger{36} - %msg%n</pattern> | |
| </encoder> | |
| </appender> | |
| <appender name="prod" class="ch.qos.logback.core.ConsoleAppender"> | |
| <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | |
| <layout class="com.wire.bots.echo.CustomLoggingLayout"/> | |
| </encoder> | |
| </appender> | |
| <if condition='"${JSON_LOGGING:-false}".equalsIgnoreCase("true")'> | |
| <then> | |
| <root level="INFO"> | |
| <appender-ref ref="prod"/> | |
| </root> | |
| </then> | |
| <else> | |
| <root level="DEBUG"> | |
| <appender-ref ref="local"/> | |
| </root> | |
| </else> | |
| </if> | |
| <logger name="org.eclipse.jetty" level="INFO"/> | |
| <logger name="io.netty" level="INFO"/> | |
| <logger name="com.wire" level="DEBUG"/> | |
| </configuration> |
This file contains hidden or 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
| package com.wire.bots.echo; | |
| import ch.qos.logback.classic.LoggerContext; | |
| import ch.qos.logback.classic.util.ContextInitializer; | |
| import ch.qos.logback.core.joran.spi.JoranException; | |
| import com.codahale.metrics.MetricRegistry; | |
| import com.fasterxml.jackson.annotation.JsonIgnore; | |
| import io.dropwizard.logging.LoggingFactory; | |
| import io.dropwizard.logging.LoggingUtil; | |
| /** | |
| * https://github.com/dropwizard/dropwizard/issues/1567 | |
| * Override getLoggingFactory for your configuration | |
| */ | |
| public class LogbackAutoConfigLoggingFactory implements LoggingFactory { | |
| @JsonIgnore | |
| private final LoggerContext loggerContext; | |
| @JsonIgnore | |
| private final ContextInitializer contextInitializer; | |
| public LogbackAutoConfigLoggingFactory() { | |
| this.loggerContext = LoggingUtil.getLoggerContext(); | |
| this.contextInitializer = new ContextInitializer(loggerContext); | |
| } | |
| @Override | |
| public void configure(MetricRegistry metricRegistry, String name) { | |
| try { | |
| contextInitializer.autoConfig(); | |
| } catch (JoranException e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| @Override | |
| public void stop() { | |
| loggerContext.stop(); | |
| } | |
| @Override | |
| public void reset() { | |
| loggerContext.reset(); | |
| } | |
| } |
This file contains hidden or 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
| <dependency> | |
| <groupId>org.codehaus.janino</groupId> | |
| <artifactId>janino</artifactId> | |
| <version>3.1.2</version> | |
| </dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment