Created
May 15, 2020 12:55
-
-
Save LukasForst/885a3167dc5209ee35305019ea500f0f to your computer and use it in GitHub Desktop.
Dropwizard with custom logback layout
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
logging: | |
level: INFO | |
appenders: | |
- type: production-stdout-json |
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
# put in META-INF/services/io.dropwizard.logging.AppenderFactory without this line | |
io.dropwizard.logging.ConsoleAppenderFactory | |
io.dropwizard.logging.FileAppenderFactory | |
io.dropwizard.logging.SyslogAppenderFactory | |
com.wire.bots.echo.ProductionAppender |
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; | |
/** | |
* Layout used on Wire production services in the ELK stack. | |
*/ | |
public class JsonLoggingLayout 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
package com.wire.bots.echo; | |
import ch.qos.logback.classic.LoggerContext; | |
import ch.qos.logback.classic.spi.ILoggingEvent; | |
import ch.qos.logback.core.Appender; | |
import ch.qos.logback.core.ConsoleAppender; | |
import com.fasterxml.jackson.annotation.JsonTypeName; | |
import io.dropwizard.logging.AbstractAppenderFactory; | |
import io.dropwizard.logging.async.AsyncAppenderFactory; | |
import io.dropwizard.logging.filter.LevelFilterFactory; | |
import io.dropwizard.logging.layout.LayoutFactory; | |
/** | |
* Production console appender using JsonLoggingLayout. | |
*/ | |
@JsonTypeName("production-stdout-json") | |
public class ProductionAppender extends AbstractAppenderFactory<ILoggingEvent> { | |
@Override | |
public Appender<ILoggingEvent> build( | |
LoggerContext context, | |
String applicationName, | |
LayoutFactory<ILoggingEvent> layoutFactory, | |
LevelFilterFactory<ILoggingEvent> levelFilterFactory, | |
AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory | |
) { | |
ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>(); | |
appender.setContext(context); | |
appender.setTarget("System.out"); | |
JsonLoggingLayout l = new JsonLoggingLayout(); | |
l.start(); | |
appender.setLayout(l); | |
appender.start(); | |
return appender; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment