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

# OpenTelemetry Logging Introduction

> Introduction to OpenTelemetry logging, covering log types, structured formats, library integrations, trace and span correlation, Collector pipelines for ingestion and processing, and logging best practices.

It's time to learn about logging in OpenTelemetry. This lesson explains what logs are, common formats, how OpenTelemetry integrates with your existing logging libraries, and where the Collector fits in a unified telemetry pipeline.

Logs are one of the three primary telemetry signals (alongside metrics and traces). They provide free-form, human-readable context that is invaluable for debugging. That flexibility is powerful but also introduces risks: inconsistent formatting and clutter when teams lack logging discipline. To make logs searchable and actionable, prefer structured logging and consistent schemas.

<Callout icon="lightbulb" color="#1CB2FE">
  Structured logs (JSON or schema-backed formats) are strongly recommended: they make logs easier to query, filter, and analyze across your observability stack.
</Callout>

## What is a log?

A log is a timestamped text record of an event — something that happened in your system at a specific point in time. Logs may be:

* Structured — follow a consistent schema (JSON is common). Predictable and machine-readable.
* Unstructured — free text and stack traces. Easy for humans, but harder for programmatic analysis.
* Semi-structured — key-value tokens or delimited fields (a compromise between the two).

Logs frequently include optional metadata to aid querying and correlation:

* Level (debug, info, warn, error)
* Source (service or component)
* Context (trace\_id, span\_id, user identifiers, etc.)

These elements make logs more meaningful and easier for machines to join with traces and metrics.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/xJ69WTUsX8gCj5ol/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Logs/OpenTelemetry-Logging-Introduction/log-definition-structured-unstructured-metadata.jpg?fit=max&auto=format&n=xJ69WTUsX8gCj5ol&q=85&s=ed68b1fcc8cdb2c45f0df9ede2af7e6b" alt="An infographic titled &#x22;Defining a Log&#x22; that defines a log as &#x22;a timestamped text record of an event.&#x22; It shows two types—Structured (recommended) and Unstructured—and a section of optional metadata fields like Level, Source, and Context." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Logs/OpenTelemetry-Logging-Introduction/log-definition-structured-unstructured-metadata.jpg" />
</Frame>

## Logging libraries and OpenTelemetry integration

OpenTelemetry does not replace your existing logging libraries or formats; it augments them. Common logging libraries by language include:

| Language | Example Libraries |
| - | - |
| Java | Log4j, Logback, `java.util.logging` |
| Python | `logging`, structlog, loguru |
| .NET | Serilog, NLog, log4net |
| JavaScript/TypeScript | Winston, Bunyan, Pino |
| Go | Zap, Logrus, `log` |
| Ruby | Logger, SemanticLogger |
| PHP | Monolog |
| Elixir/Erlang | Logger |

<Callout icon="warning" color="#FF6B6B">
  OpenTelemetry enriches and interoperates with logging frameworks — it does not seek to replace them. Use OTel to add context (trace/span IDs) and to export logs consistently.
</Callout>

A typical integration flow:

1. Your application and its libraries emit log messages (info/warn/error) to a logging framework (e.g., Log4j).
2. An OpenTelemetry appender/integration captures each log record and queries the OpenTelemetry Context API.
3. If a log is produced while a span is active, the integration attaches trace\_id and span\_id to the log.
4. The enriched record passes to the OpenTelemetry Log API/SDK, which uses a LoggerProvider to create logger instances, a LogRecordProcessor to batch/filter/transform, and a LogRecordExporter to convert/export the record (commonly using OTLP).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/xJ69WTUsX8gCj5ol/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Logs/OpenTelemetry-Logging-Introduction/opentelemetry-logs-integration-diagram.jpg?fit=max&auto=format&n=xJ69WTUsX8gCj5ol&q=85&s=e5a09f5f2109d2d7c75f03db678530f4" alt="A diagram titled &#x22;OpenTelemetry Logs Integration&#x22; showing application libraries emitting logs (Info/Warn/Error). The logs flow through an OpenTelemetry Log Appender to the OpenTelemetry Context API and Log API/SDK components like LoggerProvider, Logger, LogRecordProcessor, and LogRecordExporter." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Logs/OpenTelemetry-Logging-Introduction/opentelemetry-logs-integration-diagram.jpg" />
</Frame>

## The OpenTelemetry Collector in the logging pipeline

The OpenTelemetry Collector can operate as an agent (sidecar/host) or gateway. It enables you to ingest logs from external sources (file systems, syslog, Fluentd, etc.), apply processing (parsing, transforms, enrichment), and export to your backend(s). Using the Collector lets you centralize processing and reduce per-application complexity.

Common Collector pipeline elements for logs:

| Pipeline Element | Role | Examples |
| - | - | - |
| Receiver | Ingest logs from sources | `filelog`, `syslog`, OTLP |
| Processor | Transform, parse, enrich, or filter | `transform`, `batch` |
| Exporter | Send logs to storage/observability backends | OTLP exporter, vendor-specific exporters |

This separation allows you to send logs to the Collector outside of application SDKs (e.g., tailing files), while still getting unified processing and export.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/xJ69WTUsX8gCj5ol/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Logs/OpenTelemetry-Logging-Introduction/logs-opentelemetry-collector-pipeline.jpg?fit=max&auto=format&n=xJ69WTUsX8gCj5ol&q=85&s=d16be7295ee01d5de7e89fc14c9b8eb4" alt="A simple flow diagram titled &#x22;Logs in OpenTelemetry Collector&#x22; showing a log pipeline: a File/Syslog source feeds into the OpenTelemetry Collector (with receivers, a filelog receiver/parser, a transform processor, and exporters) which then sends logs outward to Backends." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Logs/OpenTelemetry-Logging-Introduction/logs-opentelemetry-collector-pipeline.jpg" />
</Frame>

## Correlating logs with traces

When your logging library is instrumented with OpenTelemetry (via SDKs or auto-instrumentation), each log entry can include the current `trace_id` and `span_id`. This cross-signal correlation enables richer troubleshooting inside observability platforms.

Example log lines enriched with trace/span identifiers:

```text theme={null}
"User login failed"    [trace_id=abc123] [span_id=xyz456]
"Order checkout"       [trace_id=def789] [span_id=uvw111]
"Payment success"      [trace_id=ghi222] [span_id=rst333]
```

Attaching IDs at write-time (via the active context) is more robust than trying to reconstruct context later.

## Common log formats and examples

Broadly, logs follow three styles: structured (preferred), semi-structured, and unstructured.

Structured logs (JSON) — predictable and machine-readable:

```json theme={null}
{
  "timestamp": "2024-08-04T12:34:56.789Z",
  "level": "INFO",
  "service": "user-authentication",
  "message": "User login successful",
  "context": { "userId": "12345" }
}
```

Common Log Format (CLF) — text-based, widely used by web servers:

```text theme={null}
127.0.0.1 - johndoe [04/Aug/2024:12:34:56 -0400] "GET /index.html HTTP/1.1" 200 1024
```

Extended / mixed formats — CLF plus a following JSON payload (requires parsing both lines):

```text theme={null}
192.168.1.1 - johndoe [04/Aug/2024:12:34:56 -0400] "POST /api HTTP/1.1" 201 2048
{"transactionId":"abcd-efgh","responseTime":150}
```

Semi-structured examples — tokenized or key-value styles:

```text theme={null}
[ERROR] 2024-08-04 12:45:23 - Failed to connect to database: Timeout expired
```

```text theme={null}
2024-08-04T12:45:23Z level=ERROR userId=12345 message="Failed Login"
```

Unstructured logs are free text and stack traces. They remain useful for quick debugging but are harder to query without parsing and enrichment.

## Best practices and recommendations

* Prefer structured logs (JSON) with a consistent schema for services.
* Always include levels and service/host identifiers.
* Attach trace\_id and span\_id where possible to enable trace-log correlation.
* Use the OpenTelemetry Collector to centralize parsing, enrichment, and export.
* Avoid logging sensitive data (PII, credentials). Filter or redact at source or in Collector processors.

Summary

* Logs complement metrics and traces by providing flexible, context-rich events.
* Structured logs improve searchability and analysis; semi-structured logs are a useful compromise.
* OpenTelemetry enriches existing logging frameworks by adding trace/span context and a consistent export pipeline.
* The Collector can ingest logs from files/syslog, transform and enrich them, and export to backends without changing application code.
* Adopt a consistent logging schema and discipline to keep logs actionable and searchable.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/xJ69WTUsX8gCj5ol/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Logs/OpenTelemetry-Logging-Introduction/opentelemetry-logs-timeline-formats-enrichment-collector.jpg?fit=max&auto=format&n=xJ69WTUsX8gCj5ol&q=85&s=c88e665a5f0a6cc88cdcb1e5450bbcd3" alt="A presentation summary slide with a colorful vertical timeline and five numbered points outlining log handling and OpenTelemetry topics — including log formats, enrichment/standardization, collector processing, and best practices." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Logs/OpenTelemetry-Logging-Introduction/opentelemetry-logs-timeline-formats-enrichment-collector.jpg" />
</Frame>

## Links and references

* OpenTelemetry Logs specification: [https://opentelemetry.io/docs/](https://opentelemetry.io/docs/) (see Logging / Logs)
* Common Log Format (CLF): [https://en.wikipedia.org/wiki/Common\_Log\_Format](https://en.wikipedia.org/wiki/Common_Log_Format)
* OpenTelemetry Collector: [https://opentelemetry.io/docs/collector/](https://opentelemetry.io/docs/collector/)
* Best practices for structured logging: [https://12factor.net/logs](https://12factor.net/logs)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/prep-course-opentelemetry-certified-associate-certification-otca/module/021c9c6f-284e-4d7e-8cda-b202c1e3bf2c/lesson/dee2aa4e-8ee7-4924-9d43-608338adfdf1" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.