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