Java SLF4J→Logback→Logstash→Datadog— Host, Service, and Source?

Viewed 1610

Java app, built with Gradle, implementing SLF4J and Logback, exporting with Logstash to Datadog agentless logging.

Can't seem to get the host, service, or source properties to transmit:

I have a Java app, built with Gradle, implementing SLF4J and Logback, exporting with Logstash to Datadog agentless logging. Can't quite seem to get the host or service properties to transmit properly, nor the source tag. Host and Service are empty; Source is undefined

build.gradle

implementation 'ch.qos.logback:logback-classic:1.2.3'
implementation 'net.logstash.logback:logstash-logback-encoder:6.6'
implementation 'org.slf4j:log4j-over-slf4j:1.7.13'
implementation 'org.slf4j:slf4j-api:1.7.5'

logback.xml

Note where I've included the <host> and <service> tags. I also tried <property name=".." value=".."> and <KeyValuePair key="service" value="java-app" /> to no avail.

<appender name="JSON_TCP" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
  <remoteHost>intake.logs.datadoghq.com</remoteHost>
  <service>my-favorite-service</service>
  <host>${HOSTNAME}</host>
  <port>10514</port>
  <keepAliveDuration>20 seconds</keepAliveDuration>
  <encoder class="net.logstash.logback.encoder.LogstashEncoder">
    <prefix class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
      <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>abc123abc123abc123abc123abc123 %mdc{keyThatDoesNotExist}</pattern>
      </layout>
    </prefix>
  </encoder>
</appender>

Docs

Here are the docs I'm reading from Datadog:

Also, the docs for logstash-logback-encoder itself states:

By default, each property of Logback's Context (ch.qos.logback.core.Context) will appear as a field in the LoggingEvent.

By default, each property of Logback's Context (ch.qos.logback.core.Context) will appear as a field in the LoggingEvent.

So, how do I add a property to Logback's Context?

1 Answers

This is what I've got so far. Everything but the source.

Sending env, hostname, and service to datadog

import ch.qos.logback.classic.LoggerContext;

// Add context to logs
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.setPackagingDataEnabled(true);
lc.putProperty("source", "java");
lc.putProperty("service", "thing-doer");
lc.putProperty("host", "prd1.do.things"));
lc.putProperty("env", "production");
Related