{"id":43158,"library":"mongodb-log-writer","title":"MongoDB Log Writer","description":"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).","status":"active","version":"2.5.13","language":"javascript","source_language":"en","source_url":"https://github.com/mongodb-js/devtools-shared","tags":["javascript","mongodb","log","logv2","typescript"],"install":[{"cmd":"npm install mongodb-log-writer","lang":"bash","label":"npm"},{"cmd":"yarn add mongodb-log-writer","lang":"bash","label":"yarn"},{"cmd":"pnpm add mongodb-log-writer","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for serializing BSON types in log messages","package":"bson","optional":false}],"imports":[{"note":"ESM-only package; CommonJS require() will throw an error. Use dynamic import() if needed.","wrong":"const { MongoLogManager } = require('mongodb-log-writer')","symbol":"MongoLogManager","correct":"import { MongoLogManager } from 'mongodb-log-writer'"},{"note":"mongoLogId is a named export, not a default export.","wrong":"import mongoLogId from 'mongodb-log-writer'","symbol":"mongoLogId","correct":"import { mongoLogId } from 'mongodb-log-writer'"},{"note":"Use the main entry point; no sub-path exports needed.","wrong":"import { LogWriter } from 'mongodb-log-writer/esm'","symbol":"LogWriter","correct":"import { LogWriter } from 'mongodb-log-writer'"},{"note":"LogLevel is an enum exported for TypeScript usage.","wrong":null,"symbol":"LogLevel","correct":"import { LogLevel } from 'mongodb-log-writer'"}],"quickstart":{"code":"import { MongoLogManager, mongoLogId } from 'mongodb-log-writer';\nimport os from 'os';\n\nconst manager = new MongoLogManager({\n  directory: os.homedir() + '/.app-logs',\n  retentionDays: 30,\n  onwarn: (err) => console.warn('Log warning:', err),\n  onerror: (err) => console.error('Log error:', err),\n  gzip: true,\n});\n\nawait manager.cleanupOldLogfiles();\n\nconst writer = manager.createLogWriter();\nwriter.info('component', mongoLogId(12345), 'context', 'message', {\n  foo: 'bar',\n});\n\n// Flush and close when done\nwriter.flush();","lang":"typescript","description":"Shows how to create a log manager, configure log rotation with gzip, and write a logv2 message with a custom ID and metadata."},"warnings":[{"fix":"Use dynamic import() in CJS environments: import('mongodb-log-writer').then(m => ...).","message":"ESM-only: The package is ESM-only and cannot be required with CommonJS (require()).","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always use integer literals: mongoLogId(1001) not mongoLogId(someVariable).","message":"mongoLogId() must be called with a numeric literal, not a dynamic value, to ensure correct ID mapping.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use 'retentionDays' instead.","message":"The 'maxLogFileCount' option in MongoLogManager constructor was deprecated in v2.4.0.","severity":"deprecated","affected_versions":">=2.4.0"},{"fix":"Always call 'writer.flush()' or use 'await writer.flush()' before shutting down.","message":"Calling writer.flush() is necessary to ensure logs are written to disk, especially before process exit.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Install a compatible BSON version: npm install bson@^4.6.3 or ^5 etc.","message":"BSON peer dependency changed from '^4.4.0' to '^4.6.3 || ^5 || ^6.10.3 || ^7.0.0' in v2.2.0.","severity":"breaking","affected_versions":">=2.2.0"}],"env_vars":null,"search_vec":"'/devtools-shared':31 '2.5.13':22 'applic':70 'bson':94 'cadenc':81 'chang':93 'common':11 'compat':60 'compress':46 'current':18 'definit':57 'depend':92 'driver':17 'dynam':48 'follow':82 'format':65 'gzip':45 'javascript':100 'js':30 'level':50 'librari':5,53 'log':2,37,42,49,75,102 'logv2':9,64,103 'make':66 'manag':51 'messag':10 'mongodb':1,8,14,29,62,78,101 'mongodb-j':28 'monorepo':32,84 'need':72 'parsabl':76 'part':25 'peer':91 'produc':74 'provid':34 'releas':23,80 'retent':44 'rotat':43 'schedul':86 'ship':54 'stabl':19 'structur':36 'suitabl':68 'support':40 'tie':89 'tool':15,79 'type':56 'typescript':55,104 'updat':88 'use':12 'v4':95 'v5':96 'v6':97 'v7':99 'version':20 'write':7 'writer':3,38","created_at":"2026-06-05T16:58:27.580923+00:00","updated_at":"2026-06-05T16:58:27.580923+00:00","problems":[{"fix":"Change to dynamic import: const { MongoLogManager } = await import('mongodb-log-writer');","cause":"Using CommonJS require() on an ESM-only package.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported."},{"fix":"Use named import: import { mongoLogId } from 'mongodb-log-writer';","cause":"Importing mongoLogId as a default export instead of a named export.","error":"TypeError: mongoLogId is not a function"},{"fix":"Ensure it's imported as a value: import { MongoLogManager } from 'mongodb-log-writer';","cause":"Importing MongoLogManager incorrectly, e.g., as a type-only import.","error":"MongoLogManager is not a constructor"},{"fix":"Install the package: npm install mongodb-log-writer. Ensure tsconfig.json has 'moduleResolution': 'node' or 'bundler', and 'esModuleInterop': true.","cause":"Package not installed or TypeScript not configured for ESM resolution.","error":"Cannot find module 'mongodb-log-writer' or its corresponding type declarations."}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/mongodb-js/devtools-shared","github":"https://github.com/mongodb-js/devtools-shared","docs":null,"changelog":null,"pypi":null,"npm":"mongodb-log-writer","openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}