/
winston-mail.js
112 lines (94 loc) · 3.17 KB
/
winston-mail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* winston-mail.js: Transport for outputting logs to email
*
* (C) 2015 Marc Harter
* MIT LICENCE
*/
var util = require('util');
var os = require('os');
var email = require('emailjs');
var winston = require('winston');
var _ = require('underscore');
// Set Underscore to Mustache style templates
function template(text, obj) {
return _.template(text,obj, {
interpolate : /\{\{(.+?)\}\}/g
});
}
/**
* @constructs Mail
* @param {object} options hash of options
*/
var Mail = exports.Mail = function (options) {
options = options || {};
if (!options.to){
throw new Error("winston-mail requires 'to' property");
}
this.name = options.name || 'mail';
this.to = options.to;
this.from = options.from || "winston@" + os.hostname();
this.level = options.level || 'info';
this.unique = options.unique || false;
this.silent = options.silent || false;
this.subject = options.subject ? template(options.subject) : template("winston: {{level}} {{msg}}");
this.html = options.html || false; //Send mail in html format
this.handleExceptions = options.handleExceptions || false;
this.server = email.server.connect({
user : options.username,
password : options.password,
port : options.port,
host : options.host,
ssl : options.ssl || options.secure,
tls : options.tls,
timeout : options.timeout,
});
};
/** @extends winston.Transport */
util.inherits(Mail, winston.Transport);
/**
* Define a getter so that `winston.transports.Mail`
* is available and thus backwards compatible.
*/
winston.transports.Mail = Mail;
/**
* Core logging method exposed to Winston. Metadata is optional.
* @function log
* @member Mail
* @param level {string} Level at which to log the message
* @param msg {string} Message to log
* @param meta {Object} **Optional** Additional metadata to attach
* @param callback {function} Continuation to respond to when complete.
*/
Mail.prototype.log = function (level, msg, meta, callback) {
var self = this;
if (this.silent) return callback(null, true);
if (this.unique && this.level != level) return callback(null, true);
var body = msg;
// convert meta to string if it is an error
if(meta instanceof Error) {
meta = {
message: meta.message,
name: meta.name,
stack: meta.stack
};
}
// add meta info into the body if not empty
if (meta !== null && meta !== undefined && (typeof meta !== 'object' || Object.keys(meta).length > 0))
body += "\n\n" + util.inspect(meta, {depth: 5}); // add some pretty printing
var msgOptions = {
from: this.from,
to: this.to,
subject: this.subject({level: level, msg: msg.split('\n')[0]}),
text: body
};
//Send mail as html
if (this.html){
msgOptions.attachment = [{data: body , alternative:true}];
}
var message = email.message.create(msgOptions);
this.server.send(message, function (err) {
if (err) self.emit('error', err);
self.emit('logged');
callback(null, true);
});
};