Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gov.cms.bfd.pipeline.app;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.contrib.json.classic.JsonLayout;
import java.util.LinkedHashMap;
import java.util.Map;

/** Customizes logback json output. */
public class CustomJsonLayout extends JsonLayout {
@Override
protected void addCustomDataToJsonMap(Map<String, Object> map, ILoggingEvent event) {
map.put("bfd_env", System.getenv("BFD_ENV_NAME"));
// This field is added by default by logback, but we don't really need it for our purposes.
map.remove("context");
// logback uses timestamp, but the standard label is @timestamp, which also allows cloudwatch to
// use this value as the event timestamp.
// This should always be a LinkedHashMap, but check anyway out of an abundance of caution.
if (map instanceof LinkedHashMap<String, Object> linkedHashMap) {
// Put the timestamp first, for readability.
linkedHashMap.putFirst("@timestamp", map.get("timestamp"));
} else {
map.put("@timestamp", map.get("timestamp"));
}
map.remove("timestamp");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@

<!-- This appender will be sent all of the app's logging statements. -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{"yyyy-MM-dd'T'HH:mm:ss.SSSXXX", UTC} ${BFD_ENV_NAME} [%thread] %-5level %logger - %msg%n</pattern>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<!-- We output the application log as newline-delimited JSON objects (NDJSON). This allows us to easily parse and search our logs. More importantly, it pairs very nicely with Logback's MDC: values added to the MDC will be available as separate keys in the JSON log events, which makes it super easy to extract them from the logs. -->
<layout class="gov.cms.bfd.pipeline.app.CustomJsonLayout">
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<!-- If you need things pretty-printed, pipe the log into jq or something like that. -->
<prettyPrint>false</prettyPrint>
</jsonFormatter>
<!-- Add line breaks between each entry, to make tailing the log simpler (and so it's actually NDJSON). -->
<appendLineSeparator>true</appendLineSeparator>
<!-- Format timestamps per ISO8601. -->
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSZ</timestampFormat>
<timestampFormatTimezoneId>UTC</timestampFormatTimezoneId>
</layout>
</encoder>
</appender>

Expand Down
16 changes: 15 additions & 1 deletion apps/bfd-pipeline/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,21 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-core</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<!-- Required by logback-json-classic. -->
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
</dependency>

</dependencies>

<build>
<pluginManagement>
Expand Down