如何为Node.js中发生的每个错误发送电子邮件? [英] How do I send an email for every error that occurs in Node.js?
问题描述
假设我的node.js应用正在运行。如果有错误(我是说所有错误。不仅是网络错误。如果是Err出来,它就算在内),如何调用函数向我发送电子邮件?
Let's say my node.js app is running. If there is an error (I mean ALL errors. Not just the web error. If it goes to Err out, it counts) , how can I call a function to send an email to me?
基本上,在我希望它写到err.out之前,我想先向我发送一封电子邮件。
Basically, before I want it to write to err.out, I want an email sent to me.
我正在使用node.js和
I'm using node.js and express.
编辑:我知道如何发送电子邮件。我想知道的问题是如何拦截错误。
您知道发生错误时如何记录到 out.log 吗?好吧,现在,我是tail -f out.log,监视错误。我不能整天坐在家里做这件事。我希望在出现out.log时随时通过电子邮件将错误发送给我。
I know how to send an email. The question I want to know is how to intercept the error. You know how when there's an error, it logs to out.log? Well, right now, I'm tail -f out.log, monitoring for errors. I can't sit at home all day doing this. I want errors emailed to me anytime it pops up in out.log.
我希望通过电子邮件将 all 错误发送给我我。 (不仅是Express错误)。如果已记录,则希望通过电子邮件发送该错误。
I want all errors emailed to me. (Not just Express errors). If it's logged, I want that error emailed.
推荐答案
您可以将节点全局console.error()函数替换为一个实现发送电子邮件:
You could replace nodes global console.error() function with one implementation sending emails:
console.error = function(msg) {
// send email
// ...
// additionaly log
process.stderr.write(msg);
};
然后 每个库中的每个调用 to console.error()会调用您的特定实现来发送邮件;)
Then every call in every library made to console.error() would call your specific implementation send out mails ;)
这篇关于如何为Node.js中发生的每个错误发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!