logd-opentelemetry
v1.0.1
Published
Forward logd messages to the OpenTelemetry Logs API without replacing existing sinks.
Readme
logd-opentelemetry
Forward logd messages to the OpenTelemetry Logs API without replacing other sinks or initializing a LoggerProvider.
Peer dependencies: logd ≥ 5.4 (for addLogSink / removeLogSink), @opentelemetry/api, @opentelemetry/api-logs.
Dev: from the pnpm workspace root, pnpm test in lib/logd-opentelemetry runs section-tests with coverage (see section-tests Coverage); artifacts under coverage/.
Install
npm install logd-opentelemetry @opentelemetry/api @opentelemetry/api-logs logdUsage
Your application or SDK must obtain an OpenTelemetry Logger from its own provider, then register this adapter once:
import { logs } from '@opentelemetry/api-logs';
import logd from 'logd';
import { registerOtelSink, unregisterOtelSink } from 'logd-opentelemetry';
const otelLogger = logs.getLogger('my-service', '1.0.0');
registerOtelSink(logd, {
otelLogger,
// sinkId: 'logd-opentelemetry', // default
});Teardown (removes only this named sink; does not shut down the provider):
unregisterOtelSink(logd);Multiple sinks
registerOtelSink uses logd.addLogSink with a unique id (sinkId, default logd-opentelemetry). Other code can call addLogSink for unrelated sinks; each receives every LogMessage without knowing the others.
- Second registration with the same
sinkIdthrows (same as logd). UseunregisterOtelSinkfirst or passforce: trueto replace only this adapter’s registration. - Legacy
registerLogSinkon the same instance is unchanged: OTel is an extra named sink and does not replace it.
Behavior
Structured body: one argument is serialized as one structured value; multiple arguments become
{ parts: [...] }(not a single concatenated string).Errors: serialized with
name,message,stack, and optionalcause.Unsafe values: circular references and other edge cases are handled without throwing out of the sink.
Severity: mapped from logd’s level to
SeverityNumber/severityText.Attributes:
logd.module,logd.level,logd.level_value; semanticcode.filepath,code.lineno,code.functionwhen callsite exists (Node).Trace:
LogRecord.contextis set tocontext.active()at emit time so the SDK can correlate with the current span when present.@opentelemetry/apiships a no-opContextManagerby default, socontext.with(...)does not propagate the active span until the host registers a real manager (for Node, typically@opentelemetry/context-async-hooks) viacontext.setGlobalContextManager(...). Without that, correlation still works if the Logs SDK reads context another way — but for standard async propagation, install the async-hooks context manager in the app/SDK startup.Runtime: only calls
otelLogger.emit. No exporters or provider initialization in this package.
API
| Export | Description |
|--------|-------------|
| registerOtelSink(host, options) | Attach sink via host.addLogSink (typically logd). |
| unregisterOtelSink(host, sinkId?) | host.removeLogSink(sinkId ?? default). |
| toSerializable(value, options?) | Optional reuse of the structured serializer. |
License
MIT
