{"record":{"id":"0ea308b1fec504b0","repo":"mongodb/node-mongodb-native","slug":"option-mongodblogpath-must-be-of-type-stderr","errorCode":null,"errorMessage":"Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable","messagePattern":"Option 'mongodbLogPath' must be of type 'stderr' \\| 'stdout' \\| MongoDBLogWritable","errorType":"exception","errorClass":"MongoAPIError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":1247,"sourceCode":"      throw new MongoParseError(`Cannot make WriteConcern from wtimeout`);\n    }\n  },\n  zlibCompressionLevel: {\n    default: 0,\n    type: 'int'\n  },\n  mongodbLogPath: {\n    transform({ values: [value] }) {\n      if (\n        !(\n          (typeof value === 'string' && ['stderr', 'stdout'].includes(value)) ||\n          (value &&\n            typeof value === 'object' &&\n            'write' in value &&\n            typeof value.write === 'function')\n        )\n      ) {\n        throw new MongoAPIError(\n          `Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable`\n        );\n      }\n      return value;\n    }\n  },\n  mongodbLogComponentSeverities: {\n    transform({ values: [value] }) {\n      if (typeof value !== 'object' || !value) {\n        throw new MongoAPIError(`Option 'mongodbLogComponentSeverities' must be a non-null object`);\n      }\n      for (const [k, v] of Object.entries(value)) {\n        if (typeof v !== 'string' || typeof k !== 'string') {\n          throw new MongoAPIError(\n            `User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value`\n          );\n        }\n        if (!Object.values(MongoLoggableComponent).some(val => val === k) && k !== 'default') {","sourceCodeStart":1229,"sourceCodeEnd":1265,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L1229-L1265","documentation":"`mongodbLogPath` selects where the driver's structured logger writes. The transform at src/connection_string.ts:1237 requires the value to be either the string `'stderr'`, the string `'stdout'`, or a non-null object exposing a `write` function (MongoDBLogWritable). Anything else — including a file path string — throws MongoAPIError at src/connection_string.ts:1247.","triggerScenarios":"Passing `mongodbLogPath: '/var/log/mongo.log'` (treating it as a file path); `mongodbLogPath: true`; `mongodbLogPath: 2` (a numeric fd).","commonSituations":"Assuming file-path semantics like pino/winston; copy-pasting a path from server logging docs; wanting to log to a file without realizing a writable stream is required.","solutions":["Use `'stderr'` or `'stdout'` for console output.","For a file, create a writable stream and pass it: `mongodbLogPath: fs.createWriteStream('/var/log/mongo.log')`.","Ensure the object you pass actually has a `write` method (e.g. a Node stream.Writable, not a string path)."],"exampleFix":"// before\nnew MongoClient(uri, { mongodbLogPath: '/var/log/mongo.log' });\n\n// after\nimport fs from 'fs';\nnew MongoClient(uri, { mongodbLogPath: fs.createWriteStream('/var/log/mongo.log') });","handlingStrategy":"type-guard","validationCode":"function isLogPath(v: unknown): boolean {\n  return v === 'stderr' || v === 'stdout' || (!!v && typeof v === 'object' && typeof (v as any).write === 'function');\n}\nif (opts.mongodbLogPath != null && !isLogPath(opts.mongodbLogPath)) {\n  throw new Error(\"mongodbLogPath must be 'stderr' | 'stdout' | MongoDBLogWritable\");\n}","typeGuard":"function isMongoDBLogWritable(v: unknown): v is { write: (...a: any[]) => any } { return !!v && typeof v === 'object' && typeof (v as any).write === 'function'; }","tryCatchPattern":"try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoAPIError && /mongodbLogPath/.test(e.message)) { opts.mongodbLogPath = 'stderr'; client = new MongoClient(uri, opts); } else throw e; }","preventionTips":["Use 'stderr'/'stdout' for console; pass a stream.Writable for files.","Never pass a file path string.","Confirm any custom writable actually implements `write`."],"tags":["logging","configuration","type-error"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}