You are viewing documentation for an outdated version. Do you wish to see documentation for the latest version?
The kamon-logback
module provides converters that can be used to put Context information in your log patterns and
bytecode instrumentation that propagates the context as expected with using Logback’s AsyncAppender
.
libraryDependencies += "io.kamon" %% "kamon-logback" % "1.0.5"
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-logback_2.13</artifactId>
<version>1.0.5</version>
</dependency>
implementation 'io.kamon:kamon-logback_2.13:1.0.5'
You must start your application with the instrumentation agent for this module to work properly.
Inserting a conversionRule
allows you to incorporate the trace ID for a request into your Logback Layout.
Here is a simple example logback.xml
configuration that does this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" debug="false">
<conversionRule conversionWord="traceID" converterClass="kamon.logback.LogbackTraceIDConverter" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5level | %traceID | %c{0} -> %m%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
If you choose to use AsyncAppender
, your trace ID will
automatically be propagated to the thread where the log is actually published. No configuration needed. The same applies
for the span ID. You can use them in the logback pattern like this:
%d{yyyy-MM-dd HH:mm:ss} | %-5level | %X{kamonTraceID} | %X{kamonSpanID} | %c{0} -> %m%n
You can also add custom values to MDC. To do this, simply add the key value in the library configuration:
kamon.logback.mdc-traced-local-keys = [ userID ].
kamon.logback.mdc-traced-broadcast-keys = [ requestID ]
Then, add the value to the kamon context:
Context
.create(Span.ContextKey, span)
.withKey(Key.broadcastString("userID"), Some("user-1"))
.withKey(Key.local[Option[String]("requestID", None), Some("request-id") {
// loggers called in this context will have access to the userID, requestID
}
Note: While in Kamon you can have one local key and one broadcast key with the same name, in MDC this is not possible. In this case only the broadcast key will be stored in MDC (will be present in the logs)