如何将process.stderr.write输出到winston

时间:2017-09-06 12:07:32

标签: node.js express winston

使用winston记录器将内容写入文件,但仅在使用customlogger.error时才有效。如果节点输出一些参考错误,如下所示

ReferenceError: aksbd is not defined
at /home/nigilan/Desktop/homepagelogger/app.js:53:20
at Layer.handle [as handle_request] (/home/nigilan/Desktop/homepagelogger/node_modules/express/lib/router/layer.js:95:5)
at next (/home/nigilan/Desktop/homepagelogger/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/nigilan/Desktop/homepagelogger/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/nigilan/Desktop/homepagelogger/node_modules/express/lib/router/layer.js:95:5)
at /home/nigilan/Desktop/homepagelogger/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/nigilan/Desktop/homepagelogger/node_modules/express/lib/router/index.js:335:12)
at next (/home/nigilan/Desktop/homepagelogger/node_modules/express/lib/router/index.js:275:10)
at /home/nigilan/Desktop/homepagelogger/app.js:38:5
at Layer.handle [as handle_request] (/home/nigilan/Desktop/homepagelogger/node_modules/express/lib/router/layer.js:95:5)

如何使用winston存储上述错误?

P.S。记录未处理的异常是nodejs服务器工作正常。

1 个答案:

答案 0 :(得分:0)

`
function formatArgs(args){
    return [util.format.apply(util.format, Array.prototype.slice.call(args))];
}
console.info = function(){
    customlogger.info.apply(customlogger, formatArgs(arguments));
};
console.warn = function(){
    customlogger.warn.apply(customlogger, formatArgs(arguments));
};`

在这里,您还可以使用winston的默认记录器而不是自定义记录器。下面的要点代码是this的完美答案。