-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathLogger.js
90 lines (68 loc) · 1.82 KB
/
Logger.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
define(function(require, exports, module) {
"use strict";
var _enabled = false,
_only = false;
function getDate() {
return (new Date()).getTime();
}
function Logger(name) {
this.name = name;
this._enabled = true;
}
Logger.prototype.factory = function(name) {
return new Logger(name);
};
Logger.prototype.log = function() {
if (!this.isEnabled()) {
return;
}
console.log.apply(console, [getDate(), this.name].concat(arguments));
};
Logger.prototype.dir = function() {
if (!this.isEnabled()) {
return;
}
console.dir.apply(console, arguments);
};
Logger.prototype.error = function() {
if (!this.isEnabled()) {
return;
}
console.error.apply(console, arguments);
};
Logger.prototype.isEnabled = function() {
return this._enabled && _enabled && (!_only || _only === this.name);
};
Logger.prototype.enable = function() {
this._enabled = true;
};
Logger.prototype.disable = function() {
this._enabled = false;
};
Logger.prototype.only = function() {
Logger._only = this.name;
};
Logger.prototype.all = function() {
Logger._only = null;
};
Logger.prototype.disableAll = function() {
Logger.disable();
};
Logger.prototype.enableAll = function() {
Logger.enable();
};
// Expose the constructor to be able to create new instances from an
// existing instance.
Logger.prototype.Logger = Logger;
Logger._enabled = typeof(console) !== 'undefined';
Logger.enable = function() {
_enabled = true;
};
Logger.disable = function() {
_enabled = false;
};
Logger.only = function(name) {
_only = name;
};
module.exports = new Logger();
});