Examples: Send OpenTelemetry Logs with the OpenTelemetry Java SDK

OpenTelemetry Java uses log appenders to intercept log messages from popular logging frameworks and converts them into OTLP logs. OpenTelemetry log appenders can be used with both the OpenTelemetry Java agent or with the OpenTelemetry Java SDK.

Example: Send OpenTelemetry Logs from a Java Application to Honeycomb 

The following example shows how to configure and run a Java application using gradle to send Log4j logs to Honeycomb or an OpenTelemetry Collector.

Acquire Dependencies 

Install the OpenTelemetry Java Agent. The log4j log appender is bundled with the java agent and will be installed automatically.

dependencies {
  // OpenTelemetry Java Agent
  agent "io.opentelemetry.javaagent:opentelemetry-javaagent:${otelAgentVersion}"
}

Acquire Dependencies 

Install Log4J and the OpenTelemetry SDK, OTLP exporter and log4j log appender packages:

dependencies {
  // Log4j
  implementation("org.apache.logging.log4j:log4j-api:2.17.2")
  implementation("org.apache.logging.log4j:log4j-core:2.17.2")

  // OpenTelemetry SDK & OTLP exporter
  implementation("io.opentelemetry:opentelemetry-sdk")
  implementation("io.opentelemetry:opentelemetry-exporter-otlp")

  // OpenTelemetry log4j appender
  implementation("io.opentelemetry.instrumentation:opentelemetry-log4j-appender-2.17")
}

Install log appender 

Then when configuring the OpenTelemetry SDK during application setup, install the OpenTelemetry log4j log appender.

import io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender;
import io.opentelemetry.sdk.OpenTelemetrySdk;

// Setup OpenTelemetry SDK
OpenTelemetrySdk sdk =
    OpenTelemetrySdk.builder()
        .build();

// Install the OpenTelemetry log4j log appender that intercepts log messages and create OTLP logs from them
OpenTelemetryAppender.install(sdk);

Configure Log4j 

Next, configure log4j. The following is an example configuration file that logs all messages to both console and the OpenTelemetry log appender. The OpenTelemetry log appender is defined in Appenders and referenced in the Root Logger (Logger > Root). Also, the packages property of the top-level Configuration element tells log4j to scan for custom appenders in the package.

<?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>

Then, in your application code you can use log4j’s LogManager to create loggers and create log messages. The following an example of creating a logger and logging a message.

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

// Use the log4j logger in your application
Logger logger = LogManager.getLogger("my-logger")
Map<String, String> mapMessage = new HashMap<>();
mapMessage.put("app.message", "Something interesting happened");
logger.info(new ObjectMessage(mapMessage));

Run 

Finally, you can run your application with some environment variables to set the service name and configure the exporter to send logs to Honeycomb:

OTEL_SERVICE_NAME="my-service" \
  OTEL_EXPORTER_OTLP_ENDPOINT="https://api.honeycomb.io" \
  OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=<your-api-key>" \
  OTEL_LOGS_EXPORTER="otlp" \
  ./gradlew run

Alternatively, you can send your logs to an OpenTelemetry collector with these environment variables:

OTEL_SERVICE_NAME="my-service" \
  OTEL_EXPORTER_OTLP_ENDPOINT="my-collector:4317" \
  OTEL_EXPORTER_OTLP_INSECURE=true \
  OTEL_LOGS_EXPORTER="otlp" \
  ./gradlew run

Log Appenders 

OpenTelemetry includes log appenders that allow the use of popular logging tools and frameworks and configures the SDK to intercept and send logs along the export pipeline.

Available log appenders: