如何针对 Node.js 中发生的每个错误发送电子邮件?
假设我的 node.js 应用程序正在运行。如果出现错误(我的意思是所有错误。不仅仅是网络错误。如果出现错误,则很重要),我如何调用函数向我发送电子邮件?
基本上,在我希望它写入 err.out 之前,我希望向我发送一封电子邮件。
我正在使用node.js 和express。
编辑:我知道如何发送电子邮件。我想知道的问题是如何拦截错误。 您知道出现错误时如何记录到 out.log 吗?好吧,现在,我正在 tail -f out.log 监视错误。我不能整天坐在家里做这个。我希望在 out.log 中出现错误时通过电子邮件发送给我。
我希望将所有错误通过电子邮件发送给我。 (不仅仅是 Express 错误)。如果已记录,我希望通过电子邮件发送该错误。
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?
Basically, before I want it to write to err.out, I want an email sent to me.
I'm using node.js and express.
Edit: 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.
I want all errors emailed to me. (Not just Express errors). If it's logged, I want that error emailed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将节点全局 console.error() 函数替换为一个发送电子邮件的实现:
然后每个库中对 console.error() 进行的每个调用都会调用您的特定实现发送发出邮件;)
You could replace nodes global console.error() function with one implementation sending emails:
Then every call in every library made to console.error() would call your specific implementation send out mails ;)
由于所有日志条目都存储在有限数量的日志文件中,而不是尝试拦截所有可能的错误事件,为什么不直接设置文件更改监视器并将新行邮寄给您呢?这样,您还可以使用过去一小时/一天/...的摘要日志条目来安排时间间隔,与打开单独的邮件相比,这将更容易浏览。
Since all log entries are stored in a limited number of log files, in stead of trying to intercept all possible error events, why not just set up a file change monitor and have the new lines mailed to you? That way you can also schedule intervals with summary log entries of the last hour/day/... which will be a lot easier to go through, compared to opening separate mails.