Winston: Logging a MongoDB ObjectID directly prints binary buffer

Created on 22 Apr 2015  路  8Comments  路  Source: winstonjs/winston

var mongodb = require('mongodb');
var oid = new mongodb.ObjectID();

var winston = require('winston');

console.info(oid);
winston.info(oid);

Result:

info:  _bsontype=ObjectID, id=U7S拢枚&1忙脷篓

This is possibly related to #500, although it was not fixed by it.

Most helpful comment

+1

using .toString() works but is extremely frustrating. any plans on fixing this?

currently doing stuff like JSON.parse(JSON.stringify(post)) 馃檲

All 8 comments

you can always just use .toString().. would be great not to have to do that though

@triccardi-systran how would we detect if it is this type? Suggests a larger failure of our serialization of custom Objects which has been a problem even with natives (RegExp and Error).

@indexzero I don't know how it could be fixed, maybe look at what other loggers do?
I confirm this is a larger issue: I already bug reported for RegExp, and I will again for mongodb 2.0 MongoError which pseudo-inherits native Error: only part of the object is logged (the stack, but not the message).

The MongoError has in fact been fixed in mongodb driver 2.0.28 by christkv/mongodb-core/pull/14.

This seems to be caused by cycle.decycle.

Modifying that function makes it work properly for me.

Added this line in there to do the magic if (value.toString && value.toString != Object.prototype.toString) { return value.toString(); }

Is there any solution for this other than patching external lib as @kaetemi said (using latest release of winston)? I log MongoDB-stored object that has some ObjectID properties like this

logger.debug('Definition saved', definition)

and for these ObjectID props I get crap like _bsontype=ObjectID, 0=88, 1=54, 2=212, 3=140, 4=168, 5=147, 6=113, 7=163, 8=107, 9=62, 10=237, 11=253 out :(

I was trying to use rewriters/filters for that but with no luck (cannot reliably detect/rewrite these props)

+1

using .toString() works but is extremely frustrating. any plans on fixing this?

currently doing stuff like JSON.parse(JSON.stringify(post)) 馃檲

In [email protected] you can serialize exotic objects like this using custom formats. Please consider upgrading.

Was this page helpful?
0 / 5 - 0 ratings