> ## Documentation Index
> Fetch the complete documentation index at: https://docs.honeycomb.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Example: Send OpenTelemetry Logs with the OpenTelemetry Java SDK

> Configure the OpenTelemetry Java SDK or Java agent to intercept log messages from your logging framework and send them to Honeycomb as structured OTLP logs.

OpenTelemetry Java uses log appenders to intercept log messages from popular logging frameworks and convert them into OTLP logs.
Log appenders work with both the OpenTelemetry Java agent and the OpenTelemetry Java SDK.

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

<Steps titleSize="h2">
  <Step title="Acquire Dependencies">
    <Tabs>
      <Tab title="Agent">
        Install the OpenTelemetry Java agent.
        The Log4j appender is bundled with the agent and installs automatically.

        ```groovy theme={}
        dependencies {
          // OpenTelemetry Java Agent
          agent "io.opentelemetry.javaagent:opentelemetry-javaagent:${otelAgentVersion}"
        }
        ```
      </Tab>

      <Tab title="SDK">
        Install Log4j and the OpenTelemetry SDK, OTLP exporter and Log4j appender packages:

        ```groovy theme={}
        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")
        }
        ```
      </Tab>
    </Tabs>
  </Step>

  <Step title="Install the Log Appender">
    <Tabs>
      <Tab title="Agent">
        The Log4j appender is bundled with the agent and requires no additional configuration.
      </Tab>

      <Tab title="SDK">
        During application setup, install the OpenTelemetry Log4j appender when configuring the SDK:

        ```java theme={}
        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);
        ```
      </Tab>
    </Tabs>
  </Step>

  <Step title="Configure Log4j">
    Configure Log4j using a configuration file.
    The following example logs all messages to both the console and the OpenTelemetry log appender.

    The `packages` property on the top-level `Configuration` element tells Log4j to scan for custom appenders in the package.
    The OpenTelemetry appender is defined in `Appenders` and referenced in the root logger (`Loggers` > `Root`).

    ```xml theme={}
    <?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 emit log messages:

    ```java theme={}
    import org.apache.logging.log4j.Logger;
    import org.apache.logging.log4j.LogManager;

    // Create a logger and use it 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));
    ```
  </Step>

  <Step title="Run Your Application">
    Create an `otelconfig.yaml` file and point the agent at it using `OTEL_CONFIG_FILE`.

    To send logs directly to Honeycomb:

    ```yaml theme={}
    file_format: "1.0"

    resource:
      attributes:
        - name: service.name
          value: ${OTEL_SERVICE_NAME:-my-service}

    logger_provider:
      processors:
        - batch:
            exporter:
              otlp_http:
                endpoint: https://api.honeycomb.io
                headers:
                  - name: x-honeycomb-team
                    value: ${HONEYCOMB_API_KEY}

    propagator:
      composite:
        - tracecontext:
        - baggage:
    ```

    ```shell theme={}
    OTEL_CONFIG_FILE=./otelconfig.yaml ./gradlew run
    ```

    To send logs to an OpenTelemetry Collector instead:

    ```yaml theme={}
    file_format: "1.0"

    resource:
      attributes:
        - name: service.name
          value: ${OTEL_SERVICE_NAME:-my-service}

    logger_provider:
      processors:
        - batch:
            exporter:
              otlp_grpc:
                endpoint: my-collector:4317
                insecure: true

    propagator:
      composite:
        - tracecontext:
        - baggage:
    ```

    ```shell theme={}
    OTEL_CONFIG_FILE=./otelconfig.yaml ./gradlew run
    ```
  </Step>
</Steps>

## Available Log Appenders

OpenTelemetry Java includes log appenders for popular logging frameworks.
Each appender intercepts log messages and routes them through the OpenTelemetry export pipeline.

Available log appenders include:

* [Log4j Appender](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/log4j/log4j-appender-2.17/library)
* [Logback](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/logback/logback-appender-1.0/library)
* [JBoss Logmanager](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/jboss-logmanager) (agent only)
