Skip to content
Send email on bunyan log record
Latest commit 7b27fea May 1, 2015 @hyjin 1.0.2


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 via gmail's SMTP service.

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

var emailStream = new EmailStream(
  // Nodemailer mailOptions
  { from: '',
    to: ''
  // 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; +; stub)
Date: Thu, 06 Feb 2014 09:14:00 GMT
Message-Id: <87f6c7df4f5d8b178e9cc798404d9a@localhost.local>
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


npm install bunyan-emailstream


Include the module

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

Create stream instance

var emailStream = new EmailStream(mailOptions, transportOptions);


  • 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

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


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.


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.


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: ' +;
    rows.push('* hostname: ' + log.hostname);
    rows.push('* 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',

Any questions about this module?

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


MIT license.

Something went wrong with that request. Please try again.