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