Registry / devops / mongodb-log-writer

mongodb-log-writer

JSON →
library2.5.13jsnpmunverified

A library for writing MongoDB logv2 messages, commonly used in MongoDB tools and drivers. Current stable version is 2.5.13, released as part of the mongodb-js/devtools-shared monorepo. It provides a structured log writer with support for log rotation, retention, gzip compression, and dynamic log level management. The library ships TypeScript type definitions and is compatible with MongoDB's logv2 format, making it suitable for applications that need to produce logs parsable by MongoDB tooling. Release cadence follows the monorepo's schedule, with updates tied to peer dependency changes (bson v4, v5, v6, or v7).

npm install mongodb-log-writer
INSTALL
IMPORT
SIG · MONGODB-LOG-WRITER
M
mongodb-log-writer
devopsjavascriptv2.5.13
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

MongoLogManager
import { MongoLogManager } from 'mongodb-log-writer'
const { MongoLogManager } = require('mongodb-log-writer')
ESM-only package; CommonJS require() will throw an error. Use dynamic import() if needed.
mongoLogId
import { mongoLogId } from 'mongodb-log-writer'
import mongoLogId from 'mongodb-log-writer'
mongoLogId is a named export, not a default export.
LogWriter
import { LogWriter } from 'mongodb-log-writer'
import { LogWriter } from 'mongodb-log-writer/esm'
Use the main entry point; no sub-path exports needed.
LogLevel
import { LogLevel } from 'mongodb-log-writer'
LogLevel is an enum exported for TypeScript usage.

Shows how to create a log manager, configure log rotation with gzip, and write a logv2 message with a custom ID and metadata.

import { MongoLogManager, mongoLogId } from 'mongodb-log-writer'; import os from 'os'; const manager = new MongoLogManager({ directory: os.homedir() + '/.app-logs', retentionDays: 30, onwarn: (err) => console.warn('Log warning:', err), onerror: (err) => console.error('Log error:', err), gzip: true, }); await manager.cleanupOldLogfiles(); const writer = manager.createLogWriter(); writer.info('component', mongoLogId(12345), 'context', 'message', { foo: 'bar', }); // Flush and close when done writer.flush();
Debug
Known issues
breakingESM-only: The package is ESM-only and cannot be required with CommonJS (require()).
fix
Use dynamic import() in CJS environments: import('mongodb-log-writer').then(m => ...).
affects: >=2.0.0
gotchamongoLogId() must be called with a numeric literal, not a dynamic value, to ensure correct ID mapping.
fix
Always use integer literals: mongoLogId(1001) not mongoLogId(someVariable).
affects: >=1.0.0
deprecatedThe 'maxLogFileCount' option in MongoLogManager constructor was deprecated in v2.4.0.
fix
Use 'retentionDays' instead.
affects: >=2.4.0
gotchaCalling writer.flush() is necessary to ensure logs are written to disk, especially before process exit.
fix
Always call 'writer.flush()' or use 'await writer.flush()' before shutting down.
affects: >=1.0.0
breakingBSON peer dependency changed from '^4.4.0' to '^4.6.3 || ^5 || ^6.10.3 || ^7.0.0' in v2.2.0.
fix
Install a compatible BSON version: npm install bson@^4.6.3 or ^5 etc.
affects: >=2.2.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.
Using CommonJS require() on an ESM-only package.
fix
Change to dynamic import: const { MongoLogManager } = await import('mongodb-log-writer');
TypeError: mongoLogId is not a function
Importing mongoLogId as a default export instead of a named export.
fix
Use named import: import { mongoLogId } from 'mongodb-log-writer';
MongoLogManager is not a constructor
Importing MongoLogManager incorrectly, e.g., as a type-only import.
fix
Ensure it's imported as a value: import { MongoLogManager } from 'mongodb-log-writer';
Cannot find module 'mongodb-log-writer' or its corresponding type declarations.
Package not installed or TypeScript not configured for ESM resolution.
fix
Install the package: npm install mongodb-log-writer. Ensure tsconfig.json has 'moduleResolution': 'node' or 'bundler', and 'esModuleInterop': true.
Upgrade
Version history
2.5.13latest on npm
Audit
Dependencies
bsonrequiredRequired for serializing BSON types in log messages
Agent activity
7 hits · last 30 days
node
6
Resources
mongodb-log-writer — npm install mongodb-log-writer · libregistry