-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
101 lines (84 loc) · 3.04 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
/* Make a proxy for an existing Winston logger that adds a prefix and/or further
* data to the log to be emitted.
*/
'use strict';
// Is a given variable an object?
function _isObject(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
// Combine objects, without overwriting keys
function _defaults(obj) {
var length = arguments.length;
if (length < 2 || obj === null) {
return obj;
}
for (var index = 1; index < length; index++) {
var source = arguments[index],
keys = _isObject(source) ? Object.keys(source) : [],
l = keys.length;
for (var i = 0; i < l; i++) {
var key = keys[i];
if (obj[key] === void 0) {
obj[key] = source[key];
}
}
}
return obj;
}
function WinstonContext(logger, prefix, metadata) {
this._parent = logger;
// Catch: Is the parent already a proxy?
while (this._parent._parent) {
this._parent = this._parent._parent;
}
this._prefix = prefix ? (prefix.replace(/^\.|\.$/g, '') + '.') : '';
this._metadata = metadata || {};
// Generate convenience log methods based on what the parent has
var that = this;
if (this._parent && this._parent.levels && _isObject(this._parent.levels)) {
Object.keys(this._parent.levels).forEach(function (level) {
that[level] = function () {
// build argument list (level, msg, ... [string interpolate], [{metadata}], [callback])
var args = [level].concat(Array.prototype.slice.call(arguments));
that.log.apply(that, args);
};
});
}
}
WinstonContext.prototype.getContext = function getContext(prefix, metadata) {
return new WinstonContext(
this._parent,
this._prefix + prefix,
_defaults({}, metadata, this._metadata)
);
};
// Allow logs to be closed.
WinstonContext.prototype.close = function close(id) {
return this._parent.close(id);
};
// Proxy log function
WinstonContext.prototype.log = function log(level, name /*, metadata, callback*/) {
// Stolen procesing code from Winston itself
var args = Array.prototype.slice.call(arguments, 2); // All args except level and name
var callback = typeof args[args.length -1] === 'function' ? args.pop() : null;
var meta = {};
var nonMeta = [];
for(var i=0; i<args.length; i+=1) {
if (_isObject(args[i])) {
meta = _defaults(meta, args[i]);
} else {
nonMeta.push(args[i]);
}
}
this._parent.log.apply(this._parent,[level,this._prefix + name]
.concat(nonMeta)
.concat([ _defaults({}, meta, this._metadata),callback]));
};
// Helper function to install `getContext` on root winston logger
WinstonContext.patchWinstonWithGetContext = function patchWinstonWithGetContext(winstonInstance) {
winstonInstance.getContext = function getContext(prefix, meta) {
return new WinstonContext(winstonInstance, prefix, meta);
};
};
module.exports = WinstonContext;