我编写了一个用于处理uncaughtExceptions的中间件,该中间件工作正常,但是在那之后服务器将崩溃。 如何防止崩溃?
server.js:
const express = require('express');
const winston = require("winston");
const app = express();
//Logging is responsible to log and display errors
require('./startup/logging')();
//routes will contains all the routes list
require('./startup/routes')(app);
//PORT
const port = process.env.PORT || 3000;
app.listen(port,() => winston.info(`Listening on port ${port}....`));
logging.js
const express = require('express');
const winston = require('winston');
// require('express-async-errors');
module.exports = function() {
winston.handleExceptions(
new winston.transports.File({ filename: 'uncaughtExceptions.log' })
);
process.on('unhandledRejection', (ex) => {
throw ex;
});
winston.add(winston.transports.File, { filename: 'error.log' });
}
答案 0 :(得分:4)
默认情况下,winston将在记录uncaughtException之后退出。如果这不是您想要的行为,请设置exitOnError = false
const logger = winston.createLogger({ exitOnError: false }); // // or, like this: // logger.exitOnError = false;
通常认为,在异常发生后不退出是一种不好的做法,因为后果是不可预测的。如果只知道某些例外是可以容忍的,则可以使用谓词专门处理它们:
const ignoreWarnings = err => !(err instanceof WarningError);
const logger = winston.createLogger({ exitOnError: ignoreWarnings });