npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bunyan-emailstream

v1.0.2

Published

Send email on bunyan log record

Downloads

6

Readme

bunyan-emailstream

Send email on bunyan log record.

This module is cheap way to send email on bunyan log record using nodemailer.

Quick Usage Example

Here is a simple example to send 'fatal' level log messages to [email protected] via gmail's SMTP service.

var bunyan = require('bunyan');
var EmailStream = require('bunyan-emailstream').EmailStream;

var emailStream = new EmailStream(
  // Nodemailer mailOptions
  { from: '[email protected]',
    to: '[email protected]'
  },
  // Nodemailer transportOptions
  { type: 'SMTP',
    service: 'gmail',
    auth: {
      user: 'username',
      pass: 'password'
    }
  }
);

var myLogger = bunyan.createLogger({
    name: 'SleepBreaker',
    streams: [{
        type: 'raw', // You should use EmailStream with 'raw' type!
        stream: emailStream,
        level: 'fatal',
    }
        // Some other streams you want
    ]
});

myLogger.fatal(new Error('No sweet sleep anymore'), 'Something bad happened');

Above will send email like this

X-Mailer: Nodemailer (0.6.0; +http://github.com/andris9/nodemailer; stub)
Date: Thu, 06 Feb 2014 09:14:00 GMT
Message-Id: <[email protected]>
To: [email protected]
Subject: [FATAL] SleepBreaker/33973 on localhost.local
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

* name: SleepBreaker
* hostname: localhost.local
* pid: 33973
* time: Thu Feb 06 2014 16:59:12 GMT+0900 (JST)
* msg: Something bad happened
* err.stack: Error: No sweet sleep anymore
    at Object.<anonymous> (/Somewhere/Of/Code/badass.js:19:16)
    ...
    at node.js:902:3

Installation

npm install bunyan-emailstream

Usage

Include the module

var EmailStream = require('bunyan-emailstream').EmailStream;

Create stream instance

var emailStream = new EmailStream(mailOptions, transportOptions);

Where,

  • mailOptions is options of composing email message. See mailOptions for more detail.
  • transportOptions is options for nodemailer's createTransport except type property may be specified in the object. When type is omitted 'SENDMAIL' will be used by default. Refer transportOptions section for detailed options.

Pass to bunyan logger as a 'raw' type stream

bunyan.createLogger({
    streams: [{
        type: 'raw', // You should use EmailStream with 'raw' type!
        stream: emailStream,
        level: 'fatal', // I bet you don't want to set 'debug' level
    }
);

Email will be sent on log level you set. Below is an example of setting sending email on uncaught exception.

process.on('uncaughtException', function (err) {
    logger.fatal(err, 'Something bas happened');
    process.exit(1);
});

Configuration

mailOptions (required)

mailOptions will be passed to nodemail.transport.sendMail() when log record comes via EmailStream#write. See nodemailer document for full list of options.

transportOptions

You may need to specify transport type in the transportOptions.

  • type: (optional) transport type passed to nodemail.createTransport(). Default is 'SENDMAIL'.

type property will be extracted from the object and passed to first argument of nodemail.createTransport(type, options), the remaining object will be passed to second argument. See nodemailer document for available transport and full list of options.

Events

Event: mailSent

This event will be emitted on callback of nodemailer.transport.sendMail(). The arguments passed to event listeners are identical to responseStatus object described at nodemailer document

Event: error

In addition to any possible case of stream's error event, the error event will be emitted when nodemailer.transport.sendMail callback with error.

Message Customization

Formatting body

EmailStream#formatBody will be called in order to format body text. You may set custom formatter on module's exported formatBody or instance's method formatBody.

You can set your own formatter like this:

// Setting custom formatter on EmailStream instance.
emailStream.formatBody = function (log) {
    // log is bunyan log record object

    var rows = [];
    rows.push('* name: ' + log.name);
    rows.push('* hostname: ' + log.hostname);
    rows.push('* pid: ' + log.pid);
    rows.push('* time: ' + log.time);

    if (log.msg) {
        rows.push('* msg: ' + log.msg);
    }

    if (log.err) {
        rows.push('* err.stack: ' + log.err.stack);
    }

    return rows.join('\n');
});

Formatting subject

Just like formatting body EmailStream#formatSubject will be called in order to format subject text.

You can set your own formatter like this:

// Setting custom formatter on EmailStream instance.
emailStream.formatSubject = function (log) {
    // log is bunyan log record object

    return util.format(
        '[%s] %s/%s on %s',
        levelName(log.level),
        log.name,
        log.pid,
        log.hostname
    );
});

Any questions about this module?

  • Source code will explain much more.
  • Create some issue to poke me.

License

MIT license.