1 | /*
|
---|
2 | * loglevel - https://github.com/pimterry/loglevel
|
---|
3 | *
|
---|
4 | * Copyright (c) 2013 Tim Perry
|
---|
5 | * Licensed under the MIT license.
|
---|
6 | */
|
---|
7 | (function (root, definition) {
|
---|
8 | "use strict";
|
---|
9 | if (typeof define === 'function' && define.amd) {
|
---|
10 | define(definition);
|
---|
11 | } else if (typeof module === 'object' && module.exports) {
|
---|
12 | module.exports = definition();
|
---|
13 | } else {
|
---|
14 | root.log = definition();
|
---|
15 | }
|
---|
16 | }(this, function () {
|
---|
17 | "use strict";
|
---|
18 |
|
---|
19 | // Slightly dubious tricks to cut down minimized file size
|
---|
20 | var noop = function() {};
|
---|
21 | var undefinedType = "undefined";
|
---|
22 | var isIE = (typeof window !== undefinedType) && (typeof window.navigator !== undefinedType) && (
|
---|
23 | /Trident\/|MSIE /.test(window.navigator.userAgent)
|
---|
24 | );
|
---|
25 |
|
---|
26 | var logMethods = [
|
---|
27 | "trace",
|
---|
28 | "debug",
|
---|
29 | "info",
|
---|
30 | "warn",
|
---|
31 | "error"
|
---|
32 | ];
|
---|
33 |
|
---|
34 | // Cross-browser bind equivalent that works at least back to IE6
|
---|
35 | function bindMethod(obj, methodName) {
|
---|
36 | var method = obj[methodName];
|
---|
37 | if (typeof method.bind === 'function') {
|
---|
38 | return method.bind(obj);
|
---|
39 | } else {
|
---|
40 | try {
|
---|
41 | return Function.prototype.bind.call(method, obj);
|
---|
42 | } catch (e) {
|
---|
43 | // Missing bind shim or IE8 + Modernizr, fallback to wrapping
|
---|
44 | return function() {
|
---|
45 | return Function.prototype.apply.apply(method, [obj, arguments]);
|
---|
46 | };
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Trace() doesn't print the message in IE, so for that case we need to wrap it
|
---|
52 | function traceForIE() {
|
---|
53 | if (console.log) {
|
---|
54 | if (console.log.apply) {
|
---|
55 | console.log.apply(console, arguments);
|
---|
56 | } else {
|
---|
57 | // In old IE, native console methods themselves don't have apply().
|
---|
58 | Function.prototype.apply.apply(console.log, [console, arguments]);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | if (console.trace) console.trace();
|
---|
62 | }
|
---|
63 |
|
---|
64 | // Build the best logging method possible for this env
|
---|
65 | // Wherever possible we want to bind, not wrap, to preserve stack traces
|
---|
66 | function realMethod(methodName) {
|
---|
67 | if (methodName === 'debug') {
|
---|
68 | methodName = 'log';
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (typeof console === undefinedType) {
|
---|
72 | return false; // No method possible, for now - fixed later by enableLoggingWhenConsoleArrives
|
---|
73 | } else if (methodName === 'trace' && isIE) {
|
---|
74 | return traceForIE;
|
---|
75 | } else if (console[methodName] !== undefined) {
|
---|
76 | return bindMethod(console, methodName);
|
---|
77 | } else if (console.log !== undefined) {
|
---|
78 | return bindMethod(console, 'log');
|
---|
79 | } else {
|
---|
80 | return noop;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | // These private functions always need `this` to be set properly
|
---|
85 |
|
---|
86 | function replaceLoggingMethods(level, loggerName) {
|
---|
87 | /*jshint validthis:true */
|
---|
88 | for (var i = 0; i < logMethods.length; i++) {
|
---|
89 | var methodName = logMethods[i];
|
---|
90 | this[methodName] = (i < level) ?
|
---|
91 | noop :
|
---|
92 | this.methodFactory(methodName, level, loggerName);
|
---|
93 | }
|
---|
94 |
|
---|
95 | // Define log.log as an alias for log.debug
|
---|
96 | this.log = this.debug;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // In old IE versions, the console isn't present until you first open it.
|
---|
100 | // We build realMethod() replacements here that regenerate logging methods
|
---|
101 | function enableLoggingWhenConsoleArrives(methodName, level, loggerName) {
|
---|
102 | return function () {
|
---|
103 | if (typeof console !== undefinedType) {
|
---|
104 | replaceLoggingMethods.call(this, level, loggerName);
|
---|
105 | this[methodName].apply(this, arguments);
|
---|
106 | }
|
---|
107 | };
|
---|
108 | }
|
---|
109 |
|
---|
110 | // By default, we use closely bound real methods wherever possible, and
|
---|
111 | // otherwise we wait for a console to appear, and then try again.
|
---|
112 | function defaultMethodFactory(methodName, level, loggerName) {
|
---|
113 | /*jshint validthis:true */
|
---|
114 | return realMethod(methodName) ||
|
---|
115 | enableLoggingWhenConsoleArrives.apply(this, arguments);
|
---|
116 | }
|
---|
117 |
|
---|
118 | function Logger(name, defaultLevel, factory) {
|
---|
119 | var self = this;
|
---|
120 | var currentLevel;
|
---|
121 | defaultLevel = defaultLevel == null ? "WARN" : defaultLevel;
|
---|
122 |
|
---|
123 | var storageKey = "loglevel";
|
---|
124 | if (typeof name === "string") {
|
---|
125 | storageKey += ":" + name;
|
---|
126 | } else if (typeof name === "symbol") {
|
---|
127 | storageKey = undefined;
|
---|
128 | }
|
---|
129 |
|
---|
130 | function persistLevelIfPossible(levelNum) {
|
---|
131 | var levelName = (logMethods[levelNum] || 'silent').toUpperCase();
|
---|
132 |
|
---|
133 | if (typeof window === undefinedType || !storageKey) return;
|
---|
134 |
|
---|
135 | // Use localStorage if available
|
---|
136 | try {
|
---|
137 | window.localStorage[storageKey] = levelName;
|
---|
138 | return;
|
---|
139 | } catch (ignore) {}
|
---|
140 |
|
---|
141 | // Use session cookie as fallback
|
---|
142 | try {
|
---|
143 | window.document.cookie =
|
---|
144 | encodeURIComponent(storageKey) + "=" + levelName + ";";
|
---|
145 | } catch (ignore) {}
|
---|
146 | }
|
---|
147 |
|
---|
148 | function getPersistedLevel() {
|
---|
149 | var storedLevel;
|
---|
150 |
|
---|
151 | if (typeof window === undefinedType || !storageKey) return;
|
---|
152 |
|
---|
153 | try {
|
---|
154 | storedLevel = window.localStorage[storageKey];
|
---|
155 | } catch (ignore) {}
|
---|
156 |
|
---|
157 | // Fallback to cookies if local storage gives us nothing
|
---|
158 | if (typeof storedLevel === undefinedType) {
|
---|
159 | try {
|
---|
160 | var cookie = window.document.cookie;
|
---|
161 | var location = cookie.indexOf(
|
---|
162 | encodeURIComponent(storageKey) + "=");
|
---|
163 | if (location !== -1) {
|
---|
164 | storedLevel = /^([^;]+)/.exec(cookie.slice(location))[1];
|
---|
165 | }
|
---|
166 | } catch (ignore) {}
|
---|
167 | }
|
---|
168 |
|
---|
169 | // If the stored level is not valid, treat it as if nothing was stored.
|
---|
170 | if (self.levels[storedLevel] === undefined) {
|
---|
171 | storedLevel = undefined;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return storedLevel;
|
---|
175 | }
|
---|
176 |
|
---|
177 | function clearPersistedLevel() {
|
---|
178 | if (typeof window === undefinedType || !storageKey) return;
|
---|
179 |
|
---|
180 | // Use localStorage if available
|
---|
181 | try {
|
---|
182 | window.localStorage.removeItem(storageKey);
|
---|
183 | return;
|
---|
184 | } catch (ignore) {}
|
---|
185 |
|
---|
186 | // Use session cookie as fallback
|
---|
187 | try {
|
---|
188 | window.document.cookie =
|
---|
189 | encodeURIComponent(storageKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
|
---|
190 | } catch (ignore) {}
|
---|
191 | }
|
---|
192 |
|
---|
193 | /*
|
---|
194 | *
|
---|
195 | * Public logger API - see https://github.com/pimterry/loglevel for details
|
---|
196 | *
|
---|
197 | */
|
---|
198 |
|
---|
199 | self.name = name;
|
---|
200 |
|
---|
201 | self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3,
|
---|
202 | "ERROR": 4, "SILENT": 5};
|
---|
203 |
|
---|
204 | self.methodFactory = factory || defaultMethodFactory;
|
---|
205 |
|
---|
206 | self.getLevel = function () {
|
---|
207 | return currentLevel;
|
---|
208 | };
|
---|
209 |
|
---|
210 | self.setLevel = function (level, persist) {
|
---|
211 | if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
|
---|
212 | level = self.levels[level.toUpperCase()];
|
---|
213 | }
|
---|
214 | if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
|
---|
215 | currentLevel = level;
|
---|
216 | if (persist !== false) { // defaults to true
|
---|
217 | persistLevelIfPossible(level);
|
---|
218 | }
|
---|
219 | replaceLoggingMethods.call(self, level, name);
|
---|
220 | if (typeof console === undefinedType && level < self.levels.SILENT) {
|
---|
221 | return "No console available for logging";
|
---|
222 | }
|
---|
223 | } else {
|
---|
224 | throw "log.setLevel() called with invalid level: " + level;
|
---|
225 | }
|
---|
226 | };
|
---|
227 |
|
---|
228 | self.setDefaultLevel = function (level) {
|
---|
229 | defaultLevel = level;
|
---|
230 | if (!getPersistedLevel()) {
|
---|
231 | self.setLevel(level, false);
|
---|
232 | }
|
---|
233 | };
|
---|
234 |
|
---|
235 | self.resetLevel = function () {
|
---|
236 | self.setLevel(defaultLevel, false);
|
---|
237 | clearPersistedLevel();
|
---|
238 | };
|
---|
239 |
|
---|
240 | self.enableAll = function(persist) {
|
---|
241 | self.setLevel(self.levels.TRACE, persist);
|
---|
242 | };
|
---|
243 |
|
---|
244 | self.disableAll = function(persist) {
|
---|
245 | self.setLevel(self.levels.SILENT, persist);
|
---|
246 | };
|
---|
247 |
|
---|
248 | // Initialize with the right level
|
---|
249 | var initialLevel = getPersistedLevel();
|
---|
250 | if (initialLevel == null) {
|
---|
251 | initialLevel = defaultLevel;
|
---|
252 | }
|
---|
253 | self.setLevel(initialLevel, false);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /*
|
---|
257 | *
|
---|
258 | * Top-level API
|
---|
259 | *
|
---|
260 | */
|
---|
261 |
|
---|
262 | var defaultLogger = new Logger();
|
---|
263 |
|
---|
264 | var _loggersByName = {};
|
---|
265 | defaultLogger.getLogger = function getLogger(name) {
|
---|
266 | if ((typeof name !== "symbol" && typeof name !== "string") || name === "") {
|
---|
267 | throw new TypeError("You must supply a name when creating a logger.");
|
---|
268 | }
|
---|
269 |
|
---|
270 | var logger = _loggersByName[name];
|
---|
271 | if (!logger) {
|
---|
272 | logger = _loggersByName[name] = new Logger(
|
---|
273 | name, defaultLogger.getLevel(), defaultLogger.methodFactory);
|
---|
274 | }
|
---|
275 | return logger;
|
---|
276 | };
|
---|
277 |
|
---|
278 | // Grab the current global log variable in case of overwrite
|
---|
279 | var _log = (typeof window !== undefinedType) ? window.log : undefined;
|
---|
280 | defaultLogger.noConflict = function() {
|
---|
281 | if (typeof window !== undefinedType &&
|
---|
282 | window.log === defaultLogger) {
|
---|
283 | window.log = _log;
|
---|
284 | }
|
---|
285 |
|
---|
286 | return defaultLogger;
|
---|
287 | };
|
---|
288 |
|
---|
289 | defaultLogger.getLoggers = function getLoggers() {
|
---|
290 | return _loggersByName;
|
---|
291 | };
|
---|
292 |
|
---|
293 | // ES6 default export, for compatibility
|
---|
294 | defaultLogger['default'] = defaultLogger;
|
---|
295 |
|
---|
296 | return defaultLogger;
|
---|
297 | }));
|
---|