-
Notifications
You must be signed in to change notification settings - Fork 1
/
loglevel.js
152 lines (130 loc) · 4.65 KB
/
loglevel.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
147
148
149
150
151
152
/*! loglevel - v1.2.0 - https://github.com/pimterry/loglevel - (c) 2014 Tim Perry - licensed MIT */
(function (root, definition) {
if (typeof module === 'object' && module.exports && typeof require === 'function') {
module.exports = definition();
} else if (typeof define === 'function' && typeof define.amd === 'object') {
define(definition);
} else {
root.log = definition();
}
}(this, function () {
var self = {};
var noop = function() {};
var undefinedType = "undefined";
function realMethod(methodName) {
if (typeof console === undefinedType) {
return false; // We can't build a real method without a console to log to
} else if (console[methodName] !== undefined) {
return bindMethod(console, methodName);
} else if (console.log !== undefined) {
return bindMethod(console, 'log');
} else {
return noop;
}
}
function bindMethod(obj, methodName) {
var method = obj[methodName];
if (typeof method.bind === 'function') {
return method.bind(obj);
} else {
try {
return Function.prototype.bind.call(method, obj);
} catch (e) {
// Missing bind shim or IE8 + Modernizr, fallback to wrapping
return function() {
return Function.prototype.apply.apply(method, [obj, arguments]);
};
}
}
}
function enableLoggingWhenConsoleArrives(methodName, level) {
return function () {
if (typeof console !== undefinedType) {
replaceLoggingMethods(level);
self[methodName].apply(self, arguments);
}
};
}
var logMethods = [
"trace",
"debug",
"info",
"warn",
"error"
];
function replaceLoggingMethods(level) {
for (var i = 0; i < logMethods.length; i++) {
var methodName = logMethods[i];
self[methodName] = (i < level) ? noop : self.methodFactory(methodName, level);
}
}
function persistLevelIfPossible(levelNum) {
var levelName = (logMethods[levelNum] || 'silent').toUpperCase();
// Use localStorage if available
try {
window.localStorage['loglevel'] = levelName;
return;
} catch (ignore) {}
// Use session cookie as fallback
try {
window.document.cookie = "loglevel=" + levelName + ";";
} catch (ignore) {}
}
function loadPersistedLevel() {
var storedLevel;
try {
storedLevel = window.localStorage['loglevel'];
} catch (ignore) {}
if (typeof storedLevel === undefinedType) {
try {
storedLevel = /loglevel=([^;]+)/.exec(window.document.cookie)[1];
} catch (ignore) {}
}
if (self.levels[storedLevel] === undefined) {
storedLevel = "WARN";
}
self.setLevel(self.levels[storedLevel]);
}
/*
*
* Public API
*
*/
self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3,
"ERROR": 4, "SILENT": 5};
self.methodFactory = function (methodName, level) {
return realMethod(methodName) ||
enableLoggingWhenConsoleArrives(methodName, level);
};
self.setLevel = function (level) {
if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
level = self.levels[level.toUpperCase()];
}
if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
persistLevelIfPossible(level);
replaceLoggingMethods(level);
if (typeof console === undefinedType && level < self.levels.SILENT) {
return "No console available for logging";
}
} else {
throw "log.setLevel() called with invalid level: " + level;
}
};
self.enableAll = function() {
self.setLevel(self.levels.TRACE);
};
self.disableAll = function() {
self.setLevel(self.levels.SILENT);
};
// Grab the current global log variable in case of overwrite
var _log = (typeof window !== undefinedType) ? window.log : undefined;
self.noConflict = function() {
if (typeof window !== undefinedType &&
window.log === self) {
window.log = _log;
}
return self;
};
loadPersistedLevel();
return self;
}));