How to get an email every time your node.js app logs an error

This was surprisingly hard to Google for, and docs left a bit to be desired.  So here’s how I got my production Node.js app to send me an email whenever it logs an error.

1. Use log4js. I picked this logging library because it’s got a vote of confidence from etherpad, is fairly light weight (few dependencies, small code base), and very flexible.

 npm install log4js

2. Install nodemailer. It’s not listed as a dependency, so it doesn’t automatically install with log4js, but it’s necessary to use the SMTP appender with log4js, which is how to send emails.

npm install nodemailer

3. Configure log4js. While it’s endlessly flexible with different ways to configure it, the method I prefer is to just have a logging.json file that I load in. log4js works with “appenders” — each appender is a destination for log messages to go, which could include the console, a file, email, or whatever else.  I set mine up to log to a console (which supervisord, under which I run node, appends to a file), and to send errors by email.  Here’s a logging config with comments — remove these comments to be valid JSON.

{
  "replaceConsole": true,     // Optional: if true, console.log 
                              // will be replaced by log4js
  "appenders": [{
    "type": "console"         // This appender just sends
                              // everything to the console.
  }, {
    "type": "logLevelFilter", // This is a recursive appender,
                              // filters log messages and
                              // sends them to its own appender.

    "level": "ERROR",         // Include only error logs.

    "appender": {             // the filter's appender, smtp
      "type": "smtp",
      "recipients": "you@example.com",
      "sender": "sender@serverhost.com",
      "sendInterval": 60,     // Batch log messages, and send via 
                              // this interval in seconds (0 sends 
                              // immediately, unbatched).
      "transport": "SMTP",
      "SMTP": {
        "host": "localhost",  // Other SMTP options here.
        "port": 25
      }
    }
  }]
}

Finally, you’re ready to go.  In your app, log errors like so:

log4js = require("log4js");
log4js.configure(__dirname + "/config/logging.json") // Path to the 
                                                     // config above
logger = log4js.getLogger();

logger.error("This will send you an email!");
logger.info("But this won't.");
Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to How to get an email every time your node.js app logs an error

  1. blak3r says:

    Thanks for this… You’re right this was surprisingly hard to find via google!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s