-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (87 loc) · 2.96 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
"use strict";
var _console = require("console");
function init(instance) {
Object.defineProperty(instance, "_logLevels", {
value: {
trace: false,
debug: false,
info: false,
warn: false,
error: false
}
});
instance.setLevel(process.env.LOG_LEVEL || "info");
}
function Caipora() {
var that = _console.Console.apply(this, arguments);
/* istanbul ignore next */
if (that) {
// If constructor enforces new operator, merge with current context
Object.assign(this, that)
}
init(this);
}
Caipora.prototype = Object.create(_console.Console.prototype);
Object.defineProperty(Caipora.prototype, "constructor", {
value: Caipora
});
Caipora.prototype.setLevel = function (level) {
level = (level || "").toLowerCase();
this._logLevels.trace = level === "trace";
this._logLevels.debug = this._logLevels.trace || level === "debug";
this._logLevels.info = this._logLevels.debug || level === "info";
this._logLevels.warn = this._logLevels.info || level === "warn";
this._logLevels.error = this._logLevels.warn || level === "error";
};
Caipora.prototype.getLevel = function () {
return this._logLevels.trace && "trace" ||
this._logLevels.debug && "debug" ||
this._logLevels.info && "info" ||
this._logLevels.warn && "warn" ||
this._logLevels.error && "error" ||
"silent"
};
Object.defineProperty(Caipora.prototype, "_log", {
value: function (level, args) {
if (args.length === 1 && typeof (args[0]) === "function") {
var computedArgs = args[0].call(undefined);
if (Array.isArray(computedArgs)) {
_console.Console.prototype[level].apply(this, computedArgs);
} else {
_console.Console.prototype[level].apply(this, [computedArgs])
}
}
else {
_console.Console.prototype[level].apply(this, args);
}
}
});
Object.defineProperty(Caipora.prototype, "_logIfEnabled", {
value: function (level, args) {
if (this._logLevels[level]) this._log(level, args);
}
});
["trace", "debug", "info", "warn", "error"].forEach(function (level) {
// Do not inherit methods that do not exist in the chain
if (_console.Console.prototype[level]) {
Caipora.prototype[level] = function () {
this._logIfEnabled(level, arguments);
};
}
});
Caipora.prototype.log = function () {
this._log("info", arguments);
}
var caipora = Object.create(_console);
Reflect.ownKeys(Caipora.prototype).forEach(function (prop) {
if (prop === "constructor") { return; }
var desc = Reflect.getOwnPropertyDescriptor(Caipora.prototype, prop);
desc.value = desc.value.bind(caipora);
Reflect.defineProperty(caipora, prop, desc);
});
caipora.Console = caipora.Caipora = Caipora;
Object.defineProperty(caipora, "constructor", {
value: Caipora
});
init(caipora);
module.exports = caipora;