$ show winstonjs/winston-mongodb
winston-mongodb is a Node.js transport plugin that lets winston write log entries to MongoDB. When loaded via `require('winston-mongodb')`, it registers `winston.transports.MongoDB`. The transport accepts a MongoDB URI, a promise resolving to a MongoClient, or an existing client/db object; it creates the target collection (optionally capped), reconciles a TTL index on `timestamp` when `expireAfterSeconds` is set, and queues log/query/stream operations until the database is ready. Logging flattens winston's `info` object into a document with timestamp, level, message, optional hostname/label, and sanitized metadata. Internally, the code has a well-tested metadata sanitizer (`lib/helpers.js`) that clones arbitrary JS objects into MongoDB-safe form: it replaces `.` and `$` in keys, preserves Date/Buffer/ObjectId, converts Error into plain objects, and replaces circular references with `[Circular]`. The transport also supports querying and streaming logs: capped collections use a tailable cursor, while non-capped collections fall back to 2-second polling. The test suite uses `mongodb-memory-server`, the `abstract-winston-transport` conformance suite, and includes tests for connection flavors (URI, promise, Mongoose) and capped collections. Maintenance is currently active with CI across Node 20/22/24 and MongoDB 7/8, but code inspection shows some drift: the `silent` option calls the callback without returning and therefore still writes the log entry, a deprecation warning still says pre-connected object support 'will be removed in v5' although the package is at v7, and the query path uses `fields`/`background` options that are legacy in the current MongoDB driver. These are manageable rough edges rather than fundamental flaws.
The LLM identified 2 files worth highlighting for reuse:
Self-contained metadata normalizer used before inserting log documents. It deep-clones arbitrary JS values, converting Error instances to plain objects, preserving Date/Buffer/ObjectId, turning functions into empty objects, replacing '.' and '$' in keys with safe alternatives, and breaking circular references with '[Circular]'. It depends only on mongodb's ObjectId and is exhaustively unit-tested, making it directly reusable in any MongoDB-bound serialization pipeline.
The transport class itself. Notable non-obvious patterns include an operation queue that buffers log/query/stream calls until MongoClient initialization completes, idempotent collection setup with capped collection options and TTL index reconciliation (recreating the index only when expireAfterSeconds changes), and dual streaming paths (tailable cursor for capped collections, timestamp-polling fallback for uncapped). While coupled to winston and mongodb by design, the implementation is self-contained and demonstrates how to build a resilient logging transport.