A warning
I think the intended use of winston is to write string messages in first place and (if necessary) additional meta-information. Moreover, I do not quite understand why you want to register the entire collection returned from mongo, and not - let them say it - just _id (assuming docs can be quite large).
Introduction
I looked at the source of winston and here are the relevant parts:
Winston / logger.js
Logger.prototype.log = function (level) { var self = this, args = Array.prototype.slice.call(arguments, 1); ... var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null, meta = typeof args[args.length - 1] === 'object' ? args.pop() : {}, msg = util.format.apply(null, args); ... }
Basically, a single argument of type object interpreted as meta. The console transport layer (by default) is mainly defined in winston / common.js, and here is how the meta is processed:
... if (Object.keys(meta).length > 0) { output += ' ' + ( options.prettyPrint ? ('\n' + util.inspect(meta, false, null, options.colorize)) : exports.serialize(meta) ); }
The serialize method iterates (recursively) through all the keys of the object to form the final line (instead of calling .toString or the like).
Suggested Solutions
Both solutions force winston to interpret one object argument not as a meta, but as a message string.
If you want to support different transport levels, they must be changed.
Change winston source code
Just unlock the repo and make the appropriate changes to the source code. There are many ways to achieve it. One ugly one might be:
meta = args.length === 1 ? {} : (typeof args[args.length - 1] === 'object' ? args.pop() : {}),
But it would be much better to add a special case in the .serialize method to make a special call if the object is a mangos model, very naive and incorrect:
else if ('_doc' in obj && 'save' in obj){ var result = []; msg += '{' for(var key in obj['_doc']){ result.push (key + ':' + obj['_doc'][key]); } msg += result.join(', '); msg += '}'; }
(Unfortunately, there is a problem with this approach, since winston makes a copy of the meta and all methods defined above in the prototype chain are lost - otherwise it would be as simple as calling obj.toJSON , and for sure it would be the most elegant and reliable solution)
Override Winston's Default Behavior
var original = winston.log; winston.log = function(){ if(arguments.length === 2){ original.call(winston, arguments[0], arguments[1], {}); } else { original.apply(winston, arguments); } }
Explanation: arguments[0] defines the level, therefore arguments[1] is the actual object to register.