admin管理员组文章数量:1404923
I'm using pm2 () in my node.js project. Also I'm sending logs of errors of the app in Logentries ().
I wonder is it possible to log uncaught exceptions from the app (when something fails badly and pm2 restarts the app for example)? I know that using process.on('uncaughtException')
is bad practice so would like to hear some suggestions.
Thanks!
I'm using pm2 (https://github./Unitech/pm2) in my node.js project. Also I'm sending logs of errors of the app in Logentries (https://logentries.).
I wonder is it possible to log uncaught exceptions from the app (when something fails badly and pm2 restarts the app for example)? I know that using process.on('uncaughtException')
is bad practice so would like to hear some suggestions.
Thanks!
Share Improve this question asked Sep 15, 2014 at 13:54 KosmetikaKosmetika 21.3k39 gold badges112 silver badges178 bronze badges1 Answer
Reset to default 7Where did you read that process.on('uncaughtException')
is a bad practice?
As long as you exit the process after logging the exception I don't see what's bad, here is an example:
process.on('uncaughtException', function(e) {
console.error('Ouch, an unhandled exception');
//I like using new Error() for my errors (1)
console.error(e instanceof Error ? e.message : e);
process.exit(1);
});
(1): Javascript Error reference
Edit
pm2-interface is now deprecated, use require('pm2')
instead. You will be able to do exactly the same as below by using bus system events.
An alternative with pm2 is to use pm2-interface and listening to the process:exit
or process:exception
events:
var ipm2 = require('pm2-interface')();
ipm2.on('ready', function() {
console.log('Connected to pm2');
ipm2.bus.on('process:exception', function(data){
console.log(data.pm2_env.name + 'had an exception');
});
});
This is really usefull when managing more than one process through a monitoring process.
You might want to check the blog post on how to build a custom pm2 logger. It can give you some ideas about monitoring processes through pm2-interface
.
本文标签: javascriptnodejsPM2 log uncaught exceptions to thirdparty service (as Logentries)Stack Overflow
版权声明:本文标题:javascript - node.js - PM2 log uncaught exceptions to third-party service (as Logentries) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744879059a2630094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论