-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
106 lines (88 loc) · 2.9 KB
/
index.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
var util = require('util'),
queue = require('./queue'),
request = require('request'),
winston = require('winston'),
webColors = {
"black": "000",
"red": "f00",
"green": "0f0",
"yellow": "ff0",
"blue": "00f",
"magenta": "f0f",
"cyan": "0ff",
"white": "fff",
"gray": "808080",
"grey": "808080"
},
Office365ConnectorHook = winston.transports.Office365ConnectorHook = function (options) {
this.name = options.name || 'office365connectorhook';
this.level = options.level || 'silly';
this.hookUrl = options.hookUrl || null;
this.webColors = options.colors || {};
this.prependLevel = options.prependLevel === undefined ? true : options.prependLevel;
this.appendMeta = options.appendMeta === undefined ? true : options.appendMeta;
this.formatter = options.formatter || null;
},
q = queue({ concurrency: 1, autostart: true });
util.inherits(Office365ConnectorHook, winston.Transport);
ensureCallback = function (cb1, cb2) {
return function () {
try {
cb1.apply(null, arguments);
}
finally {
cb2();
}
}
}
deferredExecute = function (url, payload, callback) {
return function (cb) {
var safeCallback = ensureCallback(callback, cb);
request
.post(url)
.json(payload)
.on('response', function (response) {
if (response.statusCode === 200) {
safeCallback(null, true);
return;
}
safeCallback('Server responded with ' + response.statusCode);
})
.on('error', function (error) {
safeCallback(error);
});
}
}
Office365ConnectorHook.prototype.log = function (level, msg, meta, callback) {
var color = winston.config.allColors[level],
themeColor = this.webColors[level] || webColors[color],
title = "";
var message = '';
if (this.prependLevel) {
message += '[' + level + ']\n';
}
message += msg;
if (this.appendMeta && meta) {
title = meta.title || "";
delete meta["title"];
var props = Object.getOwnPropertyNames(meta);
if (props.length) {
// http://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify#comment57014279_26199752
message += ' ```' + JSON.stringify(meta, props, 2) + '```';
}
}
if (typeof this.formatter === 'function') {
message = this.formatter({
level: level,
message: message,
meta: meta
});
}
var payload = {
title: title,
text: message,
themeColor: themeColor
};
q.push(deferredExecute(this.hookUrl, payload, callback));
};
module.exports = Office365ConnectorHook;