Collecting Application Logs Using OTEL Java Agent

You can directly send your application logs to SigNoz using Java Agent provided by OpenTelemetry. In this documentation, we will run a sample Java application with the OpenTelemetry Java agent to send logs to SigNoz.

For collecting logs, download the Java agent from here.

To send logs from a Java application, you will need to add the agent and set the appropriate environment variables.

OTEL_LOGS_EXPORTER=otlp \
OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.<region>.signoz.cloud:443" \
OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=<SIGNOZ_INGESTION_KEY>" \
  OTEL_RESOURCE_ATTRIBUTES=service.name=<app_name> \
java -javaagent:/path/opentelemetry-javaagent.jar -jar <myapp>.jar

Settings for Appender Instrumentation

You can use appender settings by passing them as arguments in the -D<property>=<value> format.

Example:

-Dotel.instrumentation.logback-appender.experimental-log-attributes=true

Logback Configuration

Logback Documentation

System PropertyTypeDefaultDescription
otel.instrumentation.logback-appender.experimental-log-attributesBooleanfalseCapture experimental log attributes like thread.name and thread.id.
otel.instrumentation.logback-appender.experimental.capture-code-attributesBooleanfalseCapture source code attributes. May impact performance.
otel.instrumentation.logback-appender.experimental.capture-marker-attributeBooleanfalseCapture Logback markers as attributes.
otel.instrumentation.logback-appender.experimental.capture-key-value-pair-attributesBooleanfalseCapture Logback key-value pairs as attributes.
otel.instrumentation.logback-appender.experimental.capture-logger-context-attributesBooleanfalseCapture Logback logger context properties as attributes.
otel.instrumentation.logback-appender.experimental.capture-mdc-attributesStringNAComma-separated list of MDC attributes to capture. Use * to capture all attributes.

Log4j Configuration

Log4j Documentation

System PropertyTypeDefaultDescription
otel.instrumentation.log4j-appender.experimental-log-attributesBooleanfalseCapture experimental log attributes like thread.name and thread.id.
otel.instrumentation.log4j-appender.experimental.capture-map-message-attributesBooleanfalseCapture MapMessage attributes.
otel.instrumentation.log4j-appender.experimental.capture-marker-attributeBooleanfalseCapture Log4j markers as attributes.
otel.instrumentation.log4j-appender.experimental.capture-mdc-attributesStringNAComma-separated list of context data attributes to capture. Use * to capture all attributes.

Example: Collecting Logs with OTEL Java Agent

  1. Clone the Spring PetClinic Repository:
    git clone https://github.com/SigNoz/spring-petclinic
    
  2. Build the application:
    ./mvnw package
    
  3. Run the application:
OTEL_LOGS_EXPORTER=otlp \
OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.{region}.signoz.cloud:443" \
OTEL_EXPORTER_OTLP_HEADERS=signoz-access-token=<SIGNOZ_INGESTION_KEY> \
OTEL_RESOURCE_ATTRIBUTES=service.name=myapp \
java -javaagent:/path/opentelemetry-javaagent.jar -jar target/*.jar

To enable settings like experimental log attributes, pass additional arguments:

OTEL_LOGS_EXPORTER=otlp \
OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.{region}.signoz.cloud:443" \
OTEL_EXPORTER_OTLP_HEADERS=signoz-access-token=<SIGNOZ_INGESTION_KEY> \
OTEL_RESOURCE_ATTRIBUTES=service.name=myapp \
java -javaagent:/path/opentelemetry-javaagent.jar \
-Dotel.instrumentation.logback-appender.experimental-log-attributes=true \
-jar target/*.jar
  1. Access the application at http://localhost:8090.
  2. Use the application to generate logs, which will be visible on the SigNoz UI.

For troubleshooting, check the troubleshooting guide.

Was this page helpful?