Skip to content

Latest commit

 

History

History
69 lines (55 loc) · 2.26 KB

File metadata and controls

69 lines (55 loc) · 2.26 KB

Appender Instrumentation for Log4j2 version 2.17 and higher

This module provides a Log4j2 appender which forwards Log4j2 log events to the OpenTelemetry Log SDK.

Quickstart

Add these dependencies to your project

Replace OPENTELEMETRY_VERSION with the latest release.

For Maven, add to your pom.xml dependencies:

<dependencies>
  <dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-log4j-appender-2.17</artifactId>
    <version>OPENTELEMETRY_VERSION</version>
    <scope>runtime</scope>
  </dependency>
</dependencies>

For Gradle, add to your dependencies:

runtimeOnly("io.opentelemetry.instrumentation:opentelemetry-log4j-appender-2.17:OPENTELEMETRY_VERSION")

Usage

The following demonstrates how you might configure the appender in your log4j.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" packages="io.opentelemetry.instrumentation.log4j.appender.v2_17">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout
          pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} trace_id: %X{trace_id} span_id: %X{span_id} trace_flags: %X{trace_flags} - %msg%n"/>
    </Console>
    <OpenTelemetry name="OpenTelemetryAppender"/>
  </Appenders>
  <Loggers>
    <Root>
      <AppenderRef ref="OpenTelemetryAppender" level="All"/>
      <AppenderRef ref="Console" level="All"/>
    </Root>
  </Loggers>
</Configuration>

Next, configure GlobalLoggerProvider with an SdkLoggerProvider in your application.

SdkLoggerProvider sdkLoggerProvider =
  SdkLoggerProvider.builder()
    .setResource(Resource.create(...))
    .addLogProcessor(...)
    .build();
GlobalLoggerProvider.set(sdkLoggerProvider);

In this example Log4j2 log events will be sent to both the console appender and the OpenTelemetryAppender, which will drop the logs until GlobalLoggerProvider.set(..) is called. Once initialized, logs will be emitted to a Logger obtained from the SdkLoggerProvider.