$ npm install @opentelemetry/instrumentation-console
This module provides automatic instrumentation for the Node.js console module, generating OpenTelemetry LogRecords for console method calls (console.log, console.error, console.warn, etc.).
npm install @opentelemetry/instrumentation-console
>=22.15.0const { NodeSDK } = require('@opentelemetry/sdk-node');
const { ConsoleInstrumentation } = require('@opentelemetry/instrumentation-console');
const sdk = new NodeSDK({
instrumentations: [new ConsoleInstrumentation()],
});
sdk.start();
// Now console calls will generate LogRecords
console.log('Hello, world!'); // severity: INFO
console.warn('Watch out!'); // severity: WARN
console.error('Something bad'); // severity: ERROR
| Console Method | Severity Number | Severity Text |
|---|---|---|
console.trace |
TRACE | trace |
console.debug |
DEBUG | debug |
console.log |
INFO | info |
console.info |
INFO | info |
console.warn |
WARN | warn |
console.error |
ERROR | error |
console.dir |
INFO | info |
| Option | Type | Default | Description |
|---|---|---|---|
logSeverity |
SeverityNumber |
— | Minimum severity level; only logs at or above this level are sent |
When a console method is called within an active span context, the emitted
LogRecord is associated with that context. The OpenTelemetry Logs SDK uses this
to populate the LogRecord's spanContext (trace ID, span ID, and trace flags),
so exported log records can be correlated with their originating trace.
This instrumentation requires @opentelemetry/api >= 1.9.1. Starting from that version, the DiagConsoleLogger saves references to the original console methods at module load time, so internal OTel diagnostic logging bypasses the instrumentation and avoids infinite loops.
Additionally, this instrumentation includes a re-entrancy guard to prevent loops from exporters that write to the console (e.g., ConsoleLogRecordExporter).
Note: Avoid using
ConsoleLogRecordExporterorConsoleSpanExportertogether with this instrumentation in production. Use network-based exporters (e.g., OTLP) instead.
Apache 2.0 - See LICENSE for more information.
© 2010 - cnpmjs.org x YWFE | Home | YWFE