cubes-bunyan-emailstream
v1.1.4
Published
Send email on bunyan log record
Downloads
3
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'screateTransport
excepttype
property may be specified in the object. Whentype
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.