防止在uncaughtExceptions之后崩溃节点js

朱尼德·安萨里(Juned Ansari)

我编写了一个用于处理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' });

}
那将是烧瓶

文档所述,

默认情况下,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 });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章