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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build/
node_modules/
coverage/
build/
.vscode/
.nyc_output/
97 changes: 97 additions & 0 deletions docs/advanced-usage/07-output-plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
layout: default
title: Output Plugins
parent: Advanced Usage
nav_order: 7
permalink: /advanced-usage/output-plugins
---

# Output Plugins

Output plugins define where and how log records are emitted. The library supports multiple active plugins simultaneously and ships with two built-in implementations.

## Plugin Management

Output plugins can be managed at runtime using the following methods:

| Method | Description |
|---|---|
| `log.addOutputPlugin(plugin)` | Adds a plugin alongside any existing ones. |
| `log.setOutputPlugins(...plugins)` | Replaces all existing plugins with the given one(s). |
| `log.getOutputPlugins()` | Returns the list of currently registered plugins. |

## StdoutOutputPlugin

The default plugin, registered automatically on startup.
It writes log records to stdout or a configured [sink function](/cf-nodejs-logging-support/advanced-usage/custom-sink-function).
It remains active unless explicitly replaced via `setOutputPlugins()`.

## OpenTelemetryLogsOutputPlugin <span class="label label-yellow">Experimental</span>

> **Note:** This plugin relies on [`@opentelemetry/api-logs`](https://www.npmjs.com/package/@opentelemetry/api-logs), which is marked as experimental by the OpenTelemetry project. Therefore consider this plugin experimental as well and be prepared for potential breaking changes in future releases.

Available since version 8.1.0. Emits log records via the [OpenTelemetry Logs API](https://opentelemetry.io/docs/specs/otel/logs/).
Only message logs are forwarded, whereas request logs are not emitted.
It requires a configured OTel SDK with a `LoggerProvider` and appropriate exporters, either via the global OTel SDK (e.g. `@opentelemetry/sdk-node`) or passed explicitly to the constructor.
The plugin itself does not initialize any OTel SDK components.

### Registering the Plugin

```js
import log, { OpenTelemetryLogsOutputPlugin } from 'cf-nodejs-logging-support';

// Keep stdout output and also emit via OTel:
log.addOutputPlugin(new OpenTelemetryLogsOutputPlugin());

// Emit via OTel only:
log.setOutputPlugins(new OpenTelemetryLogsOutputPlugin());
```

The constructor accepts the following optional parameters:

| Parameter | Type | Description |
|---|---|---|
| `loggerProvider` | `LoggerProvider` | OTel `LoggerProvider` to use. Defaults to the global provider. |
| `context` | `Context` or `(record) => Context | undefined` | OTel context to attach to emitted log records. Can be a static `Context` or a resolver function that receives the log record and returns a context. |
Comment thread
christiand93 marked this conversation as resolved.

### Including Fields as Attributes

By default, only custom fields are forwarded as OTel log attributes. This can be changed with `setIncludeFieldsAsAttributes()`:

```js
import log, { OpenTelemetryLogsOutputPlugin, FieldInclusionMode } from 'cf-nodejs-logging-support';

const plugin = new OpenTelemetryLogsOutputPlugin();
plugin.setIncludeFieldsAsAttributes(FieldInclusionMode.AllFields);

log.addOutputPlugin(plugin);
```

| Mode | Description |
|---|---|
| `FieldInclusionMode.CustomFieldsOnly` | Only custom fields are added as attributes (default) |
| `FieldInclusionMode.AllFields` | All log record fields are added as attributes |
| `FieldInclusionMode.None` | No fields are added as attributes |

### Exception Attributes

When logging an error, the plugin automatically maps error information to the standard OTel exception attributes:

| OTel Attribute | Source |
|---|---|
| `exception.type` | Error name |
| `exception.message` | Error message |
| `exception.stacktrace` | Stack trace |

### Logging Level Mapping

The plugin maps the library's log levels to OTel severity levels as follows:

| Logging Level | OTel Severity Text | OTel Severity Number |
|---|---|---|
| `error` | `ERROR` | 17 |
| `warn` | `WARN` | 13 |
| `info` | `INFO` | 9 |
| `verbose` | `DEBUG2` | 6 |
| `debug` | `DEBUG` | 5 |
| `silly` | `TRACE` | 1 |
Comment thread
christiand93 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: default
title: Winston Transport
parent: Advanced Usage
nav_order: 7
nav_order: 8
permalink: /advanced-usage/winston-transport
---

Expand Down
6 changes: 3 additions & 3 deletions docs/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ Create child loggers that inherit and extend a logging context, ideal for
tracing a single request or transaction across multiple modules.
</div>

<div class="landing-feature">
<h3>TypeScript ready</h3>
Full TypeScript typings are included.
<div class="landing-feature landing-feature-experimental">
<h3>OpenTelemetry<span class="label label-yellow">Experimental</span></h3>
Emit logs via the OpenTelemetry Logs API using the <a href="/cf-nodejs-logging-support/advanced-usage/output-plugins">built-in output plugin</a>.
</div>

<div class="landing-feature">
Expand Down
Loading
Loading