我有一个正在运行的 express 应用程序,我正在使用 PM2 使其保持事件状态并在系统出现任何错误时重新启动。 PM2 记录错误并重新启动,这非常好。 但是我需要用错误信息通知提交这个的用户。是否有任何方式、事件或触发器在重新启动发生时或错误被写入日志文件时被激活,以便我可以 try catch 它并通知实际用户?
我按照下面的建议实现,
var pm2 = require('pm2');
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
console.log("connected to pm2")
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
console.log("error in pm2 send email");
});
});
});
我可以在 output.log 中看到“connected to pm2”,但在任何地方都看不到“error in pm2 send email”。但是我看到一个错误日志,我正在触发它以在 error.log 中强制执行错误场景。
最佳答案
1) 您可以使用来自 Keymetrics 的警报系统
2) 您可以连接到 pm2 总线并捕获错误:
// alerts.js
var pm2 = require('pm2')
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
});
});
关于node.js - 有没有一种方法或触发器可以在出现错误并使用 PM2 重新启动时进行通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41974159/