With forever
:
forever -o out.log -e err.log my-script.js
see this SO answer for more details.
If you want to only log Express server errors, you could use something like the error handler generated by the Express CLI tool:
server.on('error', onError);
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
where server
is the instance created by running http.createServer(app)
.
If you want to log callback errors, you could have something like this:
brokenFunction(function(err) {
if(err) return errorHandler(err);
//...
});
function errorHandler(err) {
//Do something with the error like printing to console or writing to file.
console.log(err);
}
where brokenFunction
is the function that returns an error to the callback and errorHandler
is the function that performs any actions you want to occur on an error.