-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (124 loc) · 3.92 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
var npmDefaults = {
'silly': true,
'debug': true,
'verbose': true,
'info': true,
'warn': true,
'error': true
},
pluckHelper = require('./lib/plucker'),
path = require('path');
module.exports = function logger(config, logspec) {
var logHelpers = {},
plucker,
pluckedInfo;
if (logspec) {
plucker = pluckHelper(logspec);
}
function setup(level) {
return function log(eventId, info, tag) {
var args;
if(arguments.length < 3) {
tag = info;
info = eventId;
eventId = null;
}
args = {
level: level,
tag: tag
};
pluckedInfo = (plucker && eventId) ? plucker.pluck(eventId, info) : info;
if(this.wrapper) {
this.wrapper(pluckedInfo, function(finalInfo) {
args.info = finalInfo;
_log(args);
});
} else {
args.info = pluckedInfo;
_log(args);
}
};
}
function _log(args) {
var level = args.level,
tag = args.tag,
info = args.info,
label = args.label;
function f(entry) {
return function(elem) {
if (elem === entry) return false;
return true;
};
}
function intersect(arr1, arr2) {
var obj = arr1.reduce(function(o, v, i) {
o[v] = true;
return o;
}, {}),
result = [];
arr2.forEach(function(entry) {
if (obj[entry]) {
result.push(entry);
}
});
return result;
}
Object.keys(config.type).forEach(function(type) {
var tags = config.type[type].level[level];
if (tags) {
//revisit figuring out whether string or array
if (tags === '*' || tags === tag) {
logHelpers[type].log(tag, level, info);
} else if(Array.isArray(tags)) {
if(!Array.isArray(tag)) {
if(!tags.every(f(tag))){
logHelpers[type].log(tag, level, info);
}
} else if(intersect(tag, tags).length > 0) {
logHelpers[type].log(tag, level, info);
}
} else if(Array.isArray(tag) && !tag.every(f(tags))) {
logHelpers[type](tag, level, info);
}
//no logging since this log unit did not require logging
}
});
}
function _enable(logger, level, doEnable) {
logger[level] = doEnable ? setup(level) : function() {};
}
//enable logging levels
var levels = npmDefaults;
if(config.levels) {
Object.keys(config.levels).forEach(function(key) {
if (levels[key] !== config.levels[key]) {
levels[key] = config.levels[key];
}
});
}
//setting up
Object.keys(config.type).forEach(function(key) {
var module;
try {
module = require(config.type[key].module);
} catch (e) {
module = require(path.join(process.cwd(), config.type[key].module));
}
logHelpers[key] = module(config.type[key], logspec, levels);
});
function Logger(wrapper) {
this.wrapper = wrapper;
}
Logger.prototype.enable = function (level, doEnable) {
this[level] = doEnable ? setup(level) : function() {};
};
Object.keys(levels).forEach(function(level) {
Logger.prototype[level] = setup(level)
});
return {
create: function(wrapper) {
return new Logger(wrapper);
}
};
};