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 |
|
---|
122 | var storageKey = "loglevel";
|
---|
123 | if (typeof name === "string") {
|
---|
124 | storageKey += ":" + name;
|
---|
125 | } else if (typeof name === "symbol") {
|
---|
126 | storageKey = undefined;
|
---|
127 | }
|
---|
128 |
|
---|
129 | function persistLevelIfPossible(levelNum) {
|
---|
130 | var levelName = (logMethods[levelNum] || 'silent').toUpperCase();
|
---|
131 |
|
---|
132 | if (typeof window === undefinedType || !storageKey) return;
|
---|
133 |
|
---|
134 | // Use localStorage if available
|
---|
135 | try {
|
---|
136 | window.localStorage[storageKey] = levelName;
|
---|
137 | return;
|
---|
138 | } catch (ignore) {}
|
---|
139 |
|
---|
140 | // Use session cookie as fallback
|
---|
141 | try {
|
---|
142 | window.document.cookie =
|
---|
143 | encodeURIComponent(storageKey) + "=" + levelName + ";";
|
---|
144 | } catch (ignore) {}
|
---|
145 | }
|
---|
146 |
|
---|
147 | function getPersistedLevel() {
|
---|
148 | var storedLevel;
|
---|
149 |
|
---|
150 | if (typeof window === undefinedType || !storageKey) return;
|
---|
151 |
|
---|
152 | try {
|
---|
153 | storedLevel = window.localStorage[storageKey];
|
---|
154 | } catch (ignore) {}
|
---|
155 |
|
---|
156 | // Fallback to cookies if local storage gives us nothing
|
---|
157 | if (typeof storedLevel === undefinedType) {
|
---|
158 | try {
|
---|
159 | var cookie = window.document.cookie;
|
---|
160 | var location = cookie.indexOf(
|
---|
161 | encodeURIComponent(storageKey) + "=");
|
---|
162 | if (location !== -1) {
|
---|
163 | storedLevel = /^([^;]+)/.exec(cookie.slice(location))[1];
|
---|
164 | }
|
---|
165 | } catch (ignore) {}
|
---|
166 | }
|
---|
167 |
|
---|
168 | // If the stored level is not valid, treat it as if nothing was stored.
|
---|
169 | if (self.levels[storedLevel] === undefined) {
|
---|
170 | storedLevel = undefined;
|
---|
171 | }
|
---|
172 |
|
---|
173 | return storedLevel;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /*
|
---|
177 | *
|
---|
178 | * Public logger API - see https://github.com/pimterry/loglevel for details
|
---|
179 | *
|
---|
180 | */
|
---|
181 |
|
---|
182 | self.name = name;
|
---|
183 |
|
---|
184 | self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3,
|
---|
185 | "ERROR": 4, "SILENT": 5};
|
---|
186 |
|
---|
187 | self.methodFactory = factory || defaultMethodFactory;
|
---|
188 |
|
---|
189 | self.getLevel = function () {
|
---|
190 | return currentLevel;
|
---|
191 | };
|
---|
192 |
|
---|
193 | self.setLevel = function (level, persist) {
|
---|
194 | if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
|
---|
195 | level = self.levels[level.toUpperCase()];
|
---|
196 | }
|
---|
197 | if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
|
---|
198 | currentLevel = level;
|
---|
199 | if (persist !== false) { // defaults to true
|
---|
200 | persistLevelIfPossible(level);
|
---|
201 | }
|
---|
202 | replaceLoggingMethods.call(self, level, name);
|
---|
203 | if (typeof console === undefinedType && level < self.levels.SILENT) {
|
---|
204 | return "No console available for logging";
|
---|
205 | }
|
---|
206 | } else {
|
---|
207 | throw "log.setLevel() called with invalid level: " + level;
|
---|
208 | }
|
---|
209 | };
|
---|
210 |
|
---|
211 | self.setDefaultLevel = function (level) {
|
---|
212 | if (!getPersistedLevel()) {
|
---|
213 | self.setLevel(level, false);
|
---|
214 | }
|
---|
215 | };
|
---|
216 |
|
---|
217 | self.enableAll = function(persist) {
|
---|
218 | self.setLevel(self.levels.TRACE, persist);
|
---|
219 | };
|
---|
220 |
|
---|
221 | self.disableAll = function(persist) {
|
---|
222 | self.setLevel(self.levels.SILENT, persist);
|
---|
223 | };
|
---|
224 |
|
---|
225 | // Initialize with the right level
|
---|
226 | var initialLevel = getPersistedLevel();
|
---|
227 | if (initialLevel == null) {
|
---|
228 | initialLevel = defaultLevel == null ? "WARN" : defaultLevel;
|
---|
229 | }
|
---|
230 | self.setLevel(initialLevel, false);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /*
|
---|
234 | *
|
---|
235 | * Top-level API
|
---|
236 | *
|
---|
237 | */
|
---|
238 |
|
---|
239 | var defaultLogger = new Logger();
|
---|
240 |
|
---|
241 | var _loggersByName = {};
|
---|
242 | defaultLogger.getLogger = function getLogger(name) {
|
---|
243 | if ((typeof name !== "symbol" && typeof name !== "string") || name === "") {
|
---|
244 | throw new TypeError("You must supply a name when creating a logger.");
|
---|
245 | }
|
---|
246 |
|
---|
247 | var logger = _loggersByName[name];
|
---|
248 | if (!logger) {
|
---|
249 | logger = _loggersByName[name] = new Logger(
|
---|
250 | name, defaultLogger.getLevel(), defaultLogger.methodFactory);
|
---|
251 | }
|
---|
252 | return logger;
|
---|
253 | };
|
---|
254 |
|
---|
255 | // Grab the current global log variable in case of overwrite
|
---|
256 | var _log = (typeof window !== undefinedType) ? window.log : undefined;
|
---|
257 | defaultLogger.noConflict = function() {
|
---|
258 | if (typeof window !== undefinedType &&
|
---|
259 | window.log === defaultLogger) {
|
---|
260 | window.log = _log;
|
---|
261 | }
|
---|
262 |
|
---|
263 | return defaultLogger;
|
---|
264 | };
|
---|
265 |
|
---|
266 | defaultLogger.getLoggers = function getLoggers() {
|
---|
267 | return _loggersByName;
|
---|
268 | };
|
---|
269 |
|
---|
270 | // ES6 default export, for compatibility
|
---|
271 | defaultLogger['default'] = defaultLogger;
|
---|
272 |
|
---|
273 | return defaultLogger;
|
---|
274 | }));
|
---|