opentelemetry-java/extensions/logging/log4j2_extensions
David Poncelow f28396b812
Log4j2 extensions (#1344)
* Initial commit of ContextDataProvider for Log4j 2.x

* add initial OpenTelemetryJsonLayout implementation

* move and reformat

* add javadoc

* update formatting

* fixes for errorprone warnings

* integrate feedback for TraceContextDataProvider

* integrate feedback for OpenTelemetryJsonLayout

* test reformat

* check span validity rather than isRecording
2020-07-01 10:27:49 -07:00
..
src Log4j2 extensions (#1344) 2020-07-01 10:27:49 -07:00
README.md Log4j2 extensions (#1344) 2020-07-01 10:27:49 -07:00
build.gradle Log4j2 extensions (#1344) 2020-07-01 10:27:49 -07:00

README.md

OpenTelemetry Extensions for Log4j 2.x

Javadocs

This module contains extensions that support adding trace correlation information to your Log4j logs. Several modules are included.

Context Data Provider

An implementation of the Log4J ContextDataProvider class is included. This class is loaded automatically via the Java ServiceProvider interface, so it works as long as this library is in your runtime classpath. This class will add three fields to the thread context for each log entry that happens within an open span. These fields can be addressed from Log4j layouts. For example, this PatternLayout configuration will add request correlation fields to your logs:

<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} traceid='%X{traceid}' spanid='%X{spanid}' traceflags='%X{traceflags}' - %msg%n"/>

Similarly request correlation flags can be added to the JsonLayout:

<JsonLayout complete="false" compact="true">
    <KeyValuePair key="traceid" value="$${ctx:traceid}"/>
    <KeyValuePair key="spanid" value="$${ctx:spanid}"/>
    <KeyValuePair key="traceflags" value="$${ctx:traceflags}"/>
</JsonLayout>

Open Telemetry JSON Layout

This module also includes a layout component, though it's output format is provisional and subject to change. To enable it, you must add io.opentelemetry.contrib.logging.log4j2 to the packages attribute of the Configuration element in your log4j configuration file. You can then use the <OpenTelemetryJsonLayout/> element as a layout. An example configuration to output to standard output would be:

<Configuration status="WARN" packages="io.opentelemetry.contrib.logging.log4j2">
  <Appenders>
    <Console name="stdout">
      <OpenTelemetryJsonLayout/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="stdout"/>
    </Root>
  </Loggers>
</Configuration>