forked from stackify/stackify-log-winston
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (49 loc) · 1.59 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
var events = require('events'),
util = require('util'),
winston = require('winston');
//
// ### function Stackify ()
// Constructor function for the Stackify transport object responsible
// for persisting log messages and metadata to a memory array of messages and sending them to Stackify API.
//
var Stackify = exports.Stackify = function (options) {
if (!options.storage) {
throw new TypeError('You have to pass Stackify logger instance');
}
winston.Transport.call(this, options);
options = options || {};
this.push = options.storage.push;
this.silent = options.silent || false;
options.storage.setLoggerName('Winston');
};
//
// Inherit from `winston.Transport`
//
util.inherits(Stackify, winston.Transport);
//
// Define a getter so that `winston.transports.Stackify`
// is available and thus backwards compatible
//
winston.transports.Stackify = Stackify;
//
// Expose the name of this Transport on the prototype
//
Stackify.prototype.name = 'stackify';
//
// ### function log (level, msg, [meta], callback)
// #### @level {string} Level at which to log the message.
// #### @msg {string} Message to log
// #### @meta {Object} **Optional** Additional metadata to attach
// #### @callback {function} Callback function.
// Core logging method exposed to Winston. Metadata is optional.
//
Stackify.prototype.log = function (level, msg, meta, callback) {
var self = this,
err = new Error();
if (this.silent) {
return callback(null, true);
}
this.push(level, msg, [meta], null, err);
self.emit('logged');
callback(null, true);
};