Logs with Javascript
Use Logs for Lumigo with our Javascript Distribution.
Overview
Lumigo's logging instrumentation for Javascript offers support for the Pino
, Bunyan
, and Winston
libraries. This integration automatically captures and forwards log data to the Lumigo platform without requiring significant code changes.
When you instrument your application with Lumigo, logs emitted via the Javascript logging module are automatically captured. If the log is created within an active span, it is enriched with trace identifiers, enabling deep observability and correlation between logs and traces. This allows for efficient debugging, improved performance monitoring, and a comprehensive view of your application's behavior.
The process is compatible with both automatic and manual instrumentation methods, allowing you to use Lumigo's enhanced logging capabilities without disrupting existing workflows.
Scope
Lumigo supports Pino
, Bunyan
and Winston
libraries when working through Javascript. Refer to this table to see which versions are supported.
Basics
Instrumentation Setup
How to work with logging on the platform
Ensure that Lumigo's OpenTelemetry distribution for Javascript is properly instrumented in your application. Follow the instructions in Instrumentation Setup to properly instrument the distribution.
To view, search, and filter logs based on these attributes, navigate to Lumigo's page.
Logging within an active span context
Ensure your application is instrumented using the Lumigo OpenTelemetry distribution. When a log is emitted within an active span, Lumigo automatically correlates the log with the corresponding trace.
export NODE_OPTIONS="${NODE_OPTIONS} -r '@lumigo/opentelemetry/sync'"
// javascript
const lumigo = require("@lumigo/opentelemetry");
// typescript
import * as lumigo from "@lumigo/opentelemetry/sync";
Benefits of logs/traces correlation:
- Improved observability by connecting logs with application traces.
- Easier troubleshooting through end-to-end visibility.
In the Lumigo platform, you can view the correlated logs under the respective trace, providing comprehensive debugging context.
To test logging with Javascript:
Neeconst { NodeSDK } = require('@opentelemetry/sdk-node');
const { BatchLogRecordProcessor } = require('@opentelemetry/sdk-logs');
const { OTLPLogExporter } = require('@opentelemetry/exporter-logs-otlp-http');
const { BunyanInstrumentation } = require('@opentelemetry/instrumentation-bunyan');
const { trace } = require('@opentelemetry/api');
const { Resource } = require('@opentelemetry/resources');
const { semanticConventions: semconv } = require('@opentelemetry/semantic-conventions');
const { BasicTracerProvider, ConsoleSpanExporter, BatchSpanProcessor, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const bunyan = require('bunyan');
const tracesExporter = new OTLPTraceExporter({
url: "https://ga-otlp.lumigo-tracer-edge.golumigo.com/v1/traces",
headers: { Authorization: `LumigoToken your_lumigo_token` },
});
const exporter = new OTLPLogExporter({
url: 'https://ga-otlp.lumigo-tracer-edge.golumigo.com/v1/logs',
headers: { Authorization: `LumigoToken your_lumigo_token` },
});
const resource = new Resource({
'service.name': 'my-bunyan-app'
});
const tracesProvider = new BasicTracerProvider({
resource: resource,
spanProcessors: [
new BatchSpanProcessor(tracesExporter),
// new SimpleSpanProcessor(new ConsoleSpanExporter()) // Only useful for troubleshooting
]
});
// Register the tracer
tracesProvider.register();
const tracer = trace.getTracer("Bunyan-Logs+tracer");
const processor = new BatchLogRecordProcessor(exporter);
const sdk = new NodeSDK({
logRecordProcessors: [processor],
instrumentations: [new BunyanInstrumentation({ logSeverity: 1 })], // Severity 1 = INFO
});
sdk.start();
const log = bunyan.createLogger({ name: 'my-bunyan-app' });
const main = async () => {
tracer.startActiveSpan("my-example-span", async span => {
log.info('Hello, Lumigo! from bunyan');
log.info({ user: 'christian', action: 'login' }, 'User logged in successfully');
span.end();
});
await Tprovider.forceFlush({ timeout: 1000 });
await processor.forceFlush();
await sdk.shutdown();
};
main();d snippet
You can also use our premade package.json
, or create your own:
{
"name": "nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@opentelemetry/api": "^1.4.1",
"@opentelemetry/exporter-logs-otlp-http": "^0.41.2",
"@opentelemetry/exporter-trace-otlp-http": "^0.41.2",
"@opentelemetry/instrumentation-bunyan": "^0.32.2",
"@opentelemetry/resources": "^1.15.2",
"@opentelemetry/sdk-logs": "^0.41.2",
"@opentelemetry/sdk-node": "^0.41.2",
"@opentelemetry/sdk-trace-base": "^1.15.2",
"@opentelemetry/semantic-conventions": "^1.30.0",
"bunyan": "^1.8.15"
}
}
Advanced Options
Troubleshooting cases where logs are not being sent
If logs are not appearing in the Lumigo platform, enable the LUMIGO_DEBUG_LOGDUMP
environment variable.
LUMIGO_DEBUG_LOGDUMP
: This functions similarly to LUMIGO_DEBUG_SPANDUMP, but for logs instead of spans. This option is only effective when LUMIGO_ENABLE_LOGS is set to true.
This outputs additional debug information to help diagnose why logs are not being sent. Check the application logs for messages prefixed with [Lumigo Log Dump]
to identify potential issues. You should also ensure the Lumigo instrumentation is correctly initialized and verify network connectivity to Lumigo's backend.
Troubleshooting cases where logs are not correlated to traces
Verify that logs are emitted within an active span, and use the Lumigo debug logs (LUMIGO_DEBUG_LOGDUMP
) to inspect log traces and diagnose correlation issues. You should also ensure the Lumigo distribution is up-to-date and correctly configured for your environment.
Cases not covered by the distribution
We do not cover every scenario with our distribution. For example, if your code has alert("something");
as opposed to console.log("something");
, the alert version will not make it to Lumigo. We do not instrument those calls, but instead instrument only calls for logger.x() if the logger comes from the logging package.
Lumigo also does not cover any logs based on non-native libraries. Make sure to only use the logging libraries we support. For example, enabling log4JS and trying to use debug and info messages:
const logger = require('log4js').getLogger();
logger.level = 'debug'; // Set log level
logger.debug("This is a debug message.");
logger.info("This is an info message.");
Will not have the logs show up in Lumigo, as Log4JS is not supported by Lumigo.
For scenarios not covered by Lumigo's distribution, we have several alternate setups you can use:
- Vanilla OpenTelemetry setup: Implement standard OpenTelemetry instrumentation for advanced use cases.
- Kubernetes log collection: Use a centralized log collector (e.g., Fluent Bit, Fluentd) to aggregate and forward logs.
Refer to Lumigo documentation for detailed guidance on integrating with these alternative setups.
Updated 4 days ago