UNPKG

debuggo

Version:

General purpose debug library based on visionmedia/debug

85 lines 2.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.promise = exports.cb = exports.namespaces = exports.getLogger = exports.enable = exports.disable = void 0; const debug = require("debug"); var debug_1 = require("debug"); Object.defineProperty(exports, "disable", { enumerable: true, get: function () { return debug_1.disable; } }); Object.defineProperty(exports, "enable", { enumerable: true, get: function () { return debug_1.enable; } }); const __namespaces = {}; const __loggers = {}; function wrap(opts, type) { const d = debug(`${opts.ns}:${type}`); if (opts.context) { const handler = { apply: function (target, thisArg, argumentList) { const [formatter, ...args] = argumentList; return target.call(thisArg, `${opts.context} ${formatter}`, ...args); }, }; return new Proxy(d, handler); } else { return d; } } function createLogger(opts) { let out = ['log', 'info', 'warn', 'error', 'debug', 'trace'].reduce((out, key) => { out[key] = wrap(opts, key); return out; }, {}); if (typeof window === 'object' && typeof window.console === 'object') { try { out.log.log = window.console.log.bind(window.console); out.info.log = window.console.info.bind(window.console); out.warn.log = window.console.warn.bind(window.console); out.error.log = window.console.error.bind(window.console); out.debug.log = (window.console.debug ? window.console.debug : window.console.log).bind(window.console); out.trace.log = (window.console.trace ? window.console.trace : window.console.log).bind(window.console); } catch (e) { } } return out; } function getLogger(nsOrOpts, context, cache) { const opts = typeof nsOrOpts === 'string' ? { ns: nsOrOpts, context, cache } : nsOrOpts; let out; if (opts.cache === false) { out = createLogger(opts); } else { let cacheKey = opts.context ? `${opts.ns}@@${opts.context}` : opts.ns; if (!__loggers[cacheKey]) { __namespaces[opts.ns] = true; __loggers[cacheKey] = createLogger(opts); } out = __loggers[cacheKey]; } return out; } exports.getLogger = getLogger; function namespaces() { return Object.keys(__namespaces); } exports.namespaces = namespaces; function cb(ns = '') { const l = getLogger(ns); return (err, data) => { if (err) { l.error(err); } else { l.info(data); } }; } exports.cb = cb; function promise(p, ns = '') { const l = getLogger(ns); return p.then(function (data) { l.info(data); }, function (err) { l.error(err); }); } exports.promise = promise; //# sourceMappingURL=index.js.map