Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 836
TIMEX Avatar asked Aug 26 '11 12:08

TIMEX


1 Answers

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);
};

Then every call in every library made to console.error() would call your specific implementation send out mails ;)

like image 164
Tobias P. Avatar answered Sep 23 '22 17:09

Tobias P.