How to output the process.stderr.write to winston

1.5k Views Asked by At

Using winston loggers to write contents into the files but that works only when customlogger.error is used. If the node is outputting some reference error like below

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)

How to use winston to store the errors like above ?

P.S. Logging the unhandled exception is nodejs server is working fine.

1

There are 1 best solutions below

0
On BEST ANSWER
`
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));
};`

Here you can also use the default logger of winston instead of your custom logger. The gist code below has the perfect answer for this.