Search code examples
javascriptnode.jsmongodbmongoosewinston

winston logger not serializing mongodb.ObjectID


I want to finally move away from console.log as a logging system and have been looking into both winston and bunyan. I have a slight preference for winston as for now I want human readable logs in longentries, and bunyan is a bit too chatty with all the extra fields.

winston is great except that it doesn't properly serialize mongo database ids.

If I do something like this:

var ObjectId = require('mongodb').ObjectID;
var winston = require('winston');
winston.info(new ObjectId());

This is what gets written to stdout:

info: _bsontype=ObjectID, id=Tø½þjÍrßgK

Sometimes I just want to dump an entire mongoose document to the log. I don't want to have to call .toString() on all my object ids?

Has anyone been able to work around this problematic scenario in winston?


Solution

  • What you are looking for is a format specifier most probably.

    You can do something like this:

    winston.info('%j', new ObjectId());

    You can read about format specifiers here http://nodejs.org/api/util.html#util_util_format_format