如何正确使用温斯顿

How to properly use Winston

我已经阅读了使用 winston 包登录 node.js 的文档。

我的问题:我是否需要将日志记录模块添加到需要 logging.. 的每个页面,或者 winston 是否以某种方式拦截 console.logconsole.error.

感谢您的宝贵时间。

通常,您需要在使用它的模块中要求您的记录器。

不过,您可以按照@spmason in the gist logging.js or what @fega suggests in his comment的建议重新定义console对象的属性:

console.log = (...args) => logger.info.call(logger, ...args);
console.info = (...args) => logger.info.call(logger, ...args);
console.warn = (...args) => logger.warn.call(logger, ...args);
console.error = (...args) => logger.error.call(logger, ...args);
console.debug = (...args) => logger.debug.call(logger, ...args);