log.js
1000 Bytes
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
var logLevel = "info";
function dummy() {}
function shouldLog(level) {
var shouldLog = (logLevel === "info" && level === "info") ||
(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning") ||
(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error");
return shouldLog;
}
function logGroup(logFn) {
return function(level, msg) {
if(shouldLog(level)) {
logFn(msg);
}
};
}
module.exports = function(level, msg) {
if(shouldLog(level)) {
if(level === "info") {
console.log(msg);
} else if(level === "warning") {
console.warn(msg);
} else if(level === "error") {
console.error(msg);
}
}
};
var group = console.group || dummy;
var groupCollapsed = console.groupCollapsed || dummy;
var groupEnd = console.groupEnd || dummy;
module.exports.group = logGroup(group);
module.exports.groupCollapsed = logGroup(groupCollapsed);
module.exports.groupEnd = logGroup(groupEnd);
module.exports.setLogLevel = function(level) {
logLevel = level;
};