Registry / aws / lambda-log

lambda-log

JSON →
library3.1.0jsnpmunverified

LambdaLog is a lightweight and performant logging library for Node.js applications, primarily optimized for AWS Lambda environments. It specializes in generating structured JSON logs, making them easily parseable and searchable by services like CloudWatch Logs and other log aggregation platforms. The current stable version is 3.1.0, released in October 2021. While a 4.x beta series was active in late 2021 introducing ESM support and TypeScript improvements, development appears to have slowed since. Key differentiators include its seamless integration with Lambda execution contexts, automatic metadata enrichment, and a robust error logging mechanism, providing a production-ready alternative to `console.log` for serverless applications. It aims for minimal overhead and provides configurable logging levels and dynamic metadata capabilities.

npm install lambda-log
INSTALL
IMPORT
SIG · LAMBDA-LOG
L
lambda-log
awsjavascriptv3.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LambdaLog
import { LambdaLog } from 'lambda-log';
const LambdaLog = require('lambda-log'); // CommonJS for v3.x, use specific import for v4+
For v3.x, CommonJS `require` is typical. For v4.x (currently in beta), ESM `import` is the intended pattern, and explicit `.js` extensions might be required for some Node.js environments.
LogMessage
import type { LogMessage } from 'lambda-log';
import { LogMessage } from 'lambda-log'; // Use 'type' for interfaces/types
When importing types or interfaces, it's best practice to use `import type` to ensure they are stripped from the JavaScript output, preventing accidental runtime imports.
LambdaLog (direct instance)
import LambdaLog from 'lambda-log/lib/LambdaLog';
import { default as LambdaLog } from 'lambda-log';
For specific advanced use cases or when targeting certain module resolutions, you might directly import the `LambdaLog` class from its internal path. The default export `import { LambdaLog } from 'lambda-log';` is generally preferred.

Demonstrates basic logging, error handling, warning messages, and custom configuration with metadata, suitable for a TypeScript AWS Lambda function.

import { LambdaLog } from 'lambda-log'; // Initialize LambdaLog with default options const log = new LambdaLog(); // Log a simple informational message with metadata log.info('Application started successfully!', { service: 'my-api', coldStart: true, version: '1.0.0' }); // Log an error caught in a try-catch block try { // Simulate an error throw new Error('Failed to process user data!'); } catch (error: any) { log.error('An unhandled exception occurred during processing', { errorMessage: error.message, errorStack: error.stack, userContext: { id: 'user-123', region: 'us-east-1' }, requestId: process.env.AWS_REQUEST_ID ?? 'no-aws-request-id' }); } // Log a warning with specific context function validateInput(input: any) { if (!input || typeof input !== 'object' || Object.keys(input).length === 0) { log.warn('Received invalid or empty input for validation.', { source: 'input-validator', inputReceived: input }); return false; } log.debug('Input validated successfully.', { keys: Object.keys(input).length }); return true; } validateInput(null); validateInput({ name: 'Alice', age: 30 }); // Configure with custom options, e.g., enabling silent mode or setting default tags const productionLog = new LambdaLog({ tags: ['production', 'critical'], silent: process.env.NODE_ENV === 'test', debug: process.env.NODE_ENV === 'development' }); productionLog.info('Production logger initialized.', 'ready for action');
Debug
Known issues
breakingVersion 4.0.0 (currently in beta) introduces significant breaking changes. While specific details aren't fully documented in the changelog, migration from v3.x will likely require code adjustments, especially concerning module imports and potentially configuration options.
fix
Refer to the official v4.0.0 release notes and migration guide once available. Avoid using beta versions in production. Stay on v3.x until a stable v4 is released and a clear upgrade path is provided.
affects: >=4.0.0-beta.1
deprecatedThe `addLevel()` method was deprecated in v3.1.0. Custom log levels should generally be avoided in favor of standard ones (info, warn, error, debug, etc.) for better compatibility with log analysis tools.
fix
Avoid using `addLevel()`. If custom log levels are critical for your workflow, consider implementing a custom formatter or pre-processing step for your logs outside of `lambda-log`.
affects: >=3.1.0
gotchaWhen migrating to Node.js ESM or using bundlers with v4.x betas, explicit `.js` file extensions in import paths might be necessary due to Node.js's module resolution rules, especially if your project relies on older tooling or configurations.
fix
Ensure your build process or Node.js environment is configured to correctly resolve ESM imports. For direct Node.js ESM usage with v4+, you may need to add `.js` extensions to your imports (e.g., `import { LambdaLog } from 'lambda-log/lib/LambdaLog.js';`).
affects: >=4.0.0-beta.2
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'EventEmitter')
Incorrect import or destructuring of `EventEmitter` within a TypeScript project, specifically in early v4 beta versions.
fix
Ensure correct module resolution and destructuring for `EventEmitter` if you're working with custom extensions that rely on it, or update to a newer beta/stable release where this fix has been applied (v4.0.0-beta.4 and later).
Error: A 'toJSON' method already exists on the Error object.
A custom error class or another library has already defined a `toJSON` method on `Error.prototype` or a specific error instance, conflicting with `lambda-log`'s attempt to add its own for serialization.
fix
Upgrade to v3.0.2 or later, which includes a fix to skip adding a `toJSON` method if one already exists. If using an older version, refactor your custom error handling to avoid direct manipulation of `toJSON` or configure `lambda-log` to not serialize errors in a conflicting manner.
ERR_MODULE_NOT_FOUND: Cannot find module 'lambda-log' (or similar ESM resolution error)
Attempting to use `lambda-log` v4.x (beta) with CommonJS `require()` or incorrect ESM import paths without `.js` extensions in a Node.js ESM context.
fix
For v4.x, ensure you are using ESM `import` statements. If experiencing resolution issues, confirm your `tsconfig.json` (if TypeScript) and `package.json` (`type: 'module'`) are correctly configured for ESM. As a workaround, try explicit `.js` extensions in your import paths: `import { LambdaLog } from 'lambda-log/lib/LambdaLog.js';`.
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
2
Resources
lambda-log — npm install lambda-log · libregistry