{"id":13423,"library":"lambda-log","title":"LambdaLog: Structured JSON Logger for Node.js & AWS Lambda","description":"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.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/KyleRoss/node-lambda-log","tags":["javascript","json","lambda","aws","log","logger","logging","console","wrap"],"install":[{"cmd":"npm install lambda-log","lang":"bash","label":"npm"},{"cmd":"yarn add lambda-log","lang":"bash","label":"yarn"},{"cmd":"pnpm add lambda-log","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const LambdaLog = require('lambda-log'); // CommonJS for v3.x, use specific import for v4+","symbol":"LambdaLog","correct":"import { LambdaLog } from 'lambda-log';"},{"note":"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.","wrong":"import { LogMessage } from 'lambda-log'; // Use 'type' for interfaces/types","symbol":"LogMessage","correct":"import type { LogMessage } from 'lambda-log';"},{"note":"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.","wrong":"import { default as LambdaLog } from 'lambda-log';","symbol":"LambdaLog (direct instance)","correct":"import LambdaLog from 'lambda-log/lib/LambdaLog';"}],"quickstart":{"code":"import { LambdaLog } from 'lambda-log';\n\n// Initialize LambdaLog with default options\nconst log = new LambdaLog();\n\n// Log a simple informational message with metadata\nlog.info('Application started successfully!', { service: 'my-api', coldStart: true, version: '1.0.0' });\n\n// Log an error caught in a try-catch block\ntry {\n  // Simulate an error\n  throw new Error('Failed to process user data!');\n} catch (error: any) {\n  log.error('An unhandled exception occurred during processing', {\n    errorMessage: error.message,\n    errorStack: error.stack,\n    userContext: { id: 'user-123', region: 'us-east-1' },\n    requestId: process.env.AWS_REQUEST_ID ?? 'no-aws-request-id'\n  });\n}\n\n// Log a warning with specific context\nfunction validateInput(input: any) {\n  if (!input || typeof input !== 'object' || Object.keys(input).length === 0) {\n    log.warn('Received invalid or empty input for validation.', { source: 'input-validator', inputReceived: input });\n    return false;\n  }\n  log.debug('Input validated successfully.', { keys: Object.keys(input).length });\n  return true;\n}\n\nvalidateInput(null);\nvalidateInput({ name: 'Alice', age: 30 });\n\n// Configure with custom options, e.g., enabling silent mode or setting default tags\nconst productionLog = new LambdaLog({\n  tags: ['production', 'critical'],\n  silent: process.env.NODE_ENV === 'test',\n  debug: process.env.NODE_ENV === 'development'\n});\nproductionLog.info('Production logger initialized.', 'ready for action');","lang":"typescript","description":"Demonstrates basic logging, error handling, warning messages, and custom configuration with metadata, suitable for a TypeScript AWS Lambda function."},"warnings":[{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=4.0.0-beta.1"},{"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`.","message":"The `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.","severity":"deprecated","affected_versions":">=3.1.0"},{"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';`).","message":"When 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.","severity":"gotcha","affected_versions":">=4.0.0-beta.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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).","cause":"Incorrect import or destructuring of `EventEmitter` within a TypeScript project, specifically in early v4 beta versions.","error":"TypeError: Cannot read properties of undefined (reading 'EventEmitter')"},{"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.","cause":"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.","error":"Error: A 'toJSON' method already exists on the Error object."},{"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';`.","cause":"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.","error":"ERR_MODULE_NOT_FOUND: Cannot find module 'lambda-log' (or similar ESM resolution error)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}