Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions content/en/docs/languages/cpp/instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,74 @@ p->AddView(std::move(observable_instrument_selector), std::move(observable_meter

## Logs

The documentation for the logs API & SDK is missing, you can help make it
available by
[editing this page](https://github.com/open-telemetry/opentelemetry.io/edit/main/content/en/docs/languages/cpp/instrumentation.md).
### Initialize exporter and processor

Initialize an exporter and processor. In this case, you initialize an OStream
Exporter that prints log records to stdout by default. The processor is
responsible for conversion of LogRecords to exportable representation before
they reach the exporter.

```cpp
auto exporter = opentelemetry::exporter::logs::OStreamLogRecordExporterFactory::Create();
auto processor =
opentelemetry::sdk::logs::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
```

### Register a logger provider

Create a `LoggerProvider` using `LoggerProviderFactory` and register it as the
global provider. Note that the factory returns a `unique_ptr`, whereas
`SetLoggerProvider` expects a `shared_ptr`. Use the provider to obtain `Logger`
objects. Loggers can be named to identify the emitting components.

```cpp
// LoggerProviderFactory::Create returns a unique_ptr;
// wrap it in a shared_ptr for SetLoggerProvider
std::shared_ptr<opentelemetry::logs::LoggerProvider> provider(
opentelemetry::sdk::logs::LoggerProviderFactory::Create(std::move(processor)));
opentelemetry::logs::Provider::SetLoggerProvider(provider);
auto logger = provider->GetLogger(name, "1.0.0");
```

### Emit a log record

Use the acquired logger to emit structured log records. The supported severity
values include: `kTrace`, `kDebug`, `kInfo`, `kWarn`, `kError`, `kFatal`.

Optionally, enrich the log records with trace context either by **implicitly**
adding the span to a scope and log within the scope, or by **explicitly**
passing trace ID, span ID, and flags.

#### With active span scope

Add the span to a scope to set the current active runtime context, and any log
records emitted within the scope automatically get the span context metadata:

```cpp
{
auto span = get_tracer()->StartSpan("HandleRequest");
auto scope = opentelemetry::trace::Scope{span};
// This log record gets trace_id, span_id, and trace_flags, etc.
// from the active scope
logger->Info("Handling request");
}
```

#### With explicit trace context

You can also pass the trace context explicitly:

```cpp
auto span = get_tracer()->StartSpan("HandleRequest");
auto ctx = span->GetContext();
logger->Info("Handling request", ctx.trace_id(), ctx.span_id(), ctx.trace_flags());
```

### Further reading

Comment thread
vitorvasc marked this conversation as resolved.
- [Logs API](/docs/specs/otel/logs/api/)
- [Logs SDK](/docs/specs/otel/logs/sdk/)
- [Sample Logs Example](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/examples/logs_simple)

## Next steps

Expand Down
12 changes: 12 additions & 0 deletions static/refcache.json
Original file line number Diff line number Diff line change
Expand Up @@ -10795,6 +10795,10 @@
"StatusCode": 206,
"LastSeen": "2026-04-22T14:11:20.437244845Z"
},
"https://github.com/open-telemetry/opentelemetry-cpp/tree/main/examples/logs_simple": {
"StatusCode": 206,
"LastSeen": "2026-04-20T17:06:33.412609523Z"
},
"https://github.com/open-telemetry/opentelemetry-cpp/tree/main/examples/metrics_simple": {
"StatusCode": 206,
"LastSeen": "2026-04-22T15:36:47.62084403Z"
Expand Down Expand Up @@ -20075,6 +20079,14 @@
"StatusCode": 200,
"LastSeen": "2026-04-22T22:57:12.897057037Z"
},
"https://opentelemetry.io/docs/specs/otel/logs/api/": {
"StatusCode": 206,
"LastSeen": "2026-04-22T00:26:10.396607883Z"
},
"https://opentelemetry.io/docs/specs/otel/logs/sdk/": {
"StatusCode": 206,
"LastSeen": "2026-04-22T00:26:13.174774163Z"
},
"https://opentelemetry.io/docs/specs/otlp/": {
"StatusCode": 206,
"LastSeen": "2026-04-22T15:43:39.509211163Z"
Expand Down