-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (140 loc) · 4.82 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
"use strict";
// ===== Plugins =====
Object.defineProperty(exports, "__esModule", { value: true });
var plugins = {
renderNamespace: function (severity, namespace) { return "[" + namespace + "]"; },
};
exports.definePlugin = {
renderNamespace: function (plugin) {
plugins.renderNamespace = plugin;
},
};
/**
* Minimum severity level of displayed messages
*/
var Level;
(function (Level) {
Level[Level["Trace"] = 0] = "Trace";
Level[Level["Log"] = 1] = "Log";
Level[Level["Warn"] = 2] = "Warn";
Level[Level["Error"] = 3] = "Error";
Level[Level["Silent"] = 4] = "Silent";
})(Level = exports.Level || (exports.Level = {}));
// ===== Settings =====
var settings = {
defaultLevel: Level.Warn,
disabled: false,
};
exports.setDefaultLevel = function (level) { settings.defaultLevel = level; };
exports.disableInProduction = function () { settings.disabled = true; };
// ===== Logger builder =====
var SEVERITIES = ['trace', 'log', 'warn', 'error'];
var consoleFactory = function (severity, namespace) {
return console[severity].bind(console, plugins.renderNamespace(severity, namespace));
};
function noop() { } // tslint:disable-line:no-empty
exports.bindTo = { consoleFactory: consoleFactory, noop: noop };
var loggerBuilder = function (namespace, level) {
return SEVERITIES.reduce(function (logger, severity, index) {
var enabled = index >= level && !settings.disabled;
logger[severity] = enabled
? exports.bindTo.consoleFactory(severity, namespace)
: exports.bindTo.noop;
// The short way - NOT IE11 compatible
// Object.assign(logger[severity], { get enabled() { return enabled; } });
// The long way - IE11 compatible
Object.defineProperty(logger[severity], 'enabled', { value: enabled, writable: false });
return logger;
}, {});
};
var Logger = /** @class */ (function () {
function Logger(ns, lvl) {
this.ns = ns;
this.lvl = lvl;
this.level = lvl;
}
Object.defineProperty(Logger.prototype, "namespace", {
get: function () {
return this.ns;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Logger.prototype, "level", {
get: function () {
return this.lvl;
},
set: function (level) {
// Note that changing the `level` programmatically will NOT update the stored level!
this.lvl = level;
// The short way - NOT IE11 compatible
// Object.assign(this, loggerBuilder(this.ns, level));
// The long way - IE11 compatible
var logger = loggerBuilder(this.ns, level);
this.trace = logger.trace;
this.log = logger.log;
this.warn = logger.warn;
this.error = logger.error;
},
enumerable: true,
configurable: true
});
return Logger;
}());
exports.Logger = Logger;
exports.getLevelState = function (config) {
return config.split(';').reduce(function (levelState, param) {
var params = param.split('=');
var namespace = params[0].trim();
var level = +params[1];
// The following is like testing: `level === Level[Level[level]]`
if (namespace && Level[level] in Level) {
levelState[namespace] = level;
}
return levelState;
}, {});
};
var getStoredLevelState = function () {
try {
var config = localStorage.getItem('NsLogger');
if (config) {
return exports.getLevelState(config);
}
}
catch (ignore) { } // tslint:disable-line:no-empty
return {};
};
exports.state = {
// Note that the stored level is only retrieved once when the script is loaded!
// When you set `localStorage.NsLogger` from the console, you have to reload the page to reflect the changes.
level: getStoredLevelState(),
logger: {},
};
exports.cleanState = function () {
exports.state.level = {};
exports.state.logger = {};
};
// ===== Get level =====
var getLevel = function (namespace) {
var level = exports.state.level[namespace];
if (level === undefined) {
var _a = namespace.split(':'), module = _a[0], feature = _a[1];
if (feature) {
level = exports.state.level[module + ":*"]; // Wildcard for all the features of a module
}
if (level === undefined) {
level = exports.state.level['*']; // Wildcard for all modules (overwrite `settings.defaultLevel`)
}
if (level === undefined) {
level = settings.defaultLevel;
}
}
return level;
};
// ===== Get logger =====
exports.getLogger = function (namespace) {
if (exports.state.logger[namespace]) {
return exports.state.logger[namespace];
}
return exports.state.logger[namespace] = new Logger(namespace, getLevel(namespace));
};