How to send an email for every error that occurs in Node.js?

Let's say my node.js. application is running If there is an error (I mean ALL errors. Not only a web error. If it goes to Err out, it is counted), how can I call a function to send an email to me?

Basically, before I want him to write err.out, I want an email to be sent to me.

I am using node.js and express.

Edit: I know how to send an email. The question I want to know is how to catch the error . Do you know how out.log log appears on error? Well, right now, I tail -f out.log, watching for errors. I can’t sit at home all day doing this. I want errors to be emailed to me anytime they appear in out.log.

I need all emails sent to me by email. (Not just Express errors). If it is registered, I want this error to be emailed.

+4
source share
3 answers

You can replace the nodes with the global console.error () function with a single email:

console.error = function(msg) { // send email // ... // additionaly log process.stderr.write(msg); }; 

Then each call in each library made for console.error () will call your specific implementation, send mail;)

+11
source

Look at nodejs clients and smtp servers

emailjs example

 var email = require("./path/to/emailjs/email"); var server = email.server.connect({ user: "username", password:"password", host: "smtp.gmail.com", ssl: true }); // send the message and get a callback with an error or details of the message that was sent server.send({ text: "i hope this works", from: "you < username@gmail.com >", to: "someone < someone@gmail.com >, another < another@gmail.com >", cc: "else < else@gmail.com >", subject: "testing emailjs" }, function(err, message) { console.log(err || message); }); 
+1
source

Since all log entries are stored in a limited number of log files, instead of trying to catch all the possible error events, why not just set up a file change monitor and send you new lines? Thus, you can also schedule intervals with summary log entries of the last hour / day / ..., which will be much easier to go through than opening individual letters.

0
source

Source: https://habr.com/ru/post/1369146/


All Articles