m-web-logger
Version:
logger util for web browser
204 lines (203 loc) • 7.88 kB
JavaScript
var i;
(function(r) {
r[r.all = 0] = "all", r[r.info = 1] = "info", r[r.warn = 2] = "warn", r[r.error = 3] = "error", r[r.slient = 4] = "slient";
})(i || (i = {}));
var j;
(function(r) {
r.level = "log_level", r.filter = "log_name";
})(j || (j = {}));
function b(r, t, l, o, u, s) {
function g(y) {
if (y !== void 0 && typeof y != "function") throw new TypeError("Function expected");
return y;
}
for (var a = o.kind, e = a === "getter" ? "get" : a === "setter" ? "set" : "value", n = !t && r ? o.static ? r : r.prototype : null, c = t || (n ? Object.getOwnPropertyDescriptor(n, o.name) : {}), f, m = !1, d = l.length - 1; d >= 0; d--) {
var v = {};
for (var p in o) v[p] = p === "access" ? {} : o[p];
for (var p in o.access) v.access[p] = o.access[p];
v.addInitializer = function(y) {
if (m) throw new TypeError("Cannot add initializers after decoration has completed");
s.push(g(y || null));
};
var h = (0, l[d])(a === "accessor" ? { get: c.get, set: c.set } : c[e], v);
if (a === "accessor") {
if (h === void 0) continue;
if (h === null || typeof h != "object") throw new TypeError("Object expected");
(f = g(h.get)) && (c.get = f), (f = g(h.set)) && (c.set = f), (f = g(h.init)) && u.unshift(f);
} else (f = g(h)) && (a === "field" ? u.unshift(f) : c[e] = f);
}
n && Object.defineProperty(n, o.name, c), m = !0;
}
function E(r, t, l) {
for (var o = arguments.length > 2, u = 0; u < t.length; u++)
l = o ? t[u].call(r, l) : t[u].call(r);
return o ? l : void 0;
}
function O(r, t, l) {
if (l || arguments.length === 2) for (var o = 0, u = t.length, s; o < u; o++)
(s || !(o in t)) && (s || (s = Array.prototype.slice.call(t, 0, o)), s[o] = t[o]);
return r.concat(s || Array.prototype.slice.call(t));
}
var w = (
/** @class */
function() {
function r(t, l, o) {
l === void 0 && (l = ""), this.level = t, this.name = l, this.data = o, this.time = /* @__PURE__ */ new Date();
}
return Object.defineProperty(r.prototype, "logTime", {
get: function() {
return this.time;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(r.prototype, "logLevel", {
get: function() {
return this.level;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(r.prototype, "logName", {
get: function() {
return this.name;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(r.prototype, "logData", {
get: function() {
return this.data;
},
enumerable: !1,
configurable: !0
}), r;
}()
);
function _(r) {
return function(t, l) {
function o() {
for (var u = [], s = 0; s < arguments.length; s++)
u[s] = arguments[s];
var g = !!k.filter, a = this.level <= r, e = k.filter.apply(k, O([this.config], u, !1));
return a && !g || a && e ? t == null ? void 0 : t.apply(this, u) : null;
}
return o;
};
}
function D(r) {
var t;
return (t = new URLSearchParams(location.search).get(r)) !== null && t !== void 0 ? t : "";
}
function S() {
var r;
return (r = i[D(j.level)]) !== null && r !== void 0 ? r : i.slient;
}
function P() {
return function(r) {
var t, l = D(j.filter);
return ((t = r.name) !== null && t !== void 0 ? t : "").includes(l);
};
}
function N() {
return function(r) {
var t, l = (t = {}, t[i.slient] = "", t[i.error] = "ERROR", t[i.warn] = "WARN", t[i.info] = "INFO", t[i.all] = "LOG", t), o = r.logTime.toLocaleString(), u = r.logLevel;
return "[".concat(o, "] [").concat(l[u], "] ").concat(r.logName ? "[".concat(r.logName, "]") : "");
};
}
function F() {
return function(r) {
return r.logData;
};
}
var k = function() {
var r, t = [], l, o, u, s, g;
return r = /** @class */
function() {
function a(e, n) {
e === void 0 && (e = {}), n === void 0 && (n = {
console,
prepend: N(),
formatData: F()
}), this.config = (E(this, t), e), this.options = n, this.level = Object.values(i).includes(this.config.level) ? this.config.level : S();
}
return Object.defineProperty(a, "filter", {
get: function() {
var e;
return (e = this._filter) !== null && e !== void 0 ? e : P();
},
set: function(e) {
this._filter = e;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(a.prototype, "name", {
get: function() {
var e;
return (e = this.config) === null || e === void 0 ? void 0 : e.name;
},
enumerable: !1,
configurable: !0
}), a.prototype.setLevel = function(e) {
this.level = e;
}, a.prototype.setName = function(e) {
this.config.name = e;
}, a.prototype.warn = function() {
for (var e = [], n = 0; n < arguments.length; n++)
e[n] = arguments[n];
return this.print(new w(i.warn, this.name, e));
}, a.prototype.log = function() {
for (var e = [], n = 0; n < arguments.length; n++)
e[n] = arguments[n];
return this.print(new w(i.all, this.name, e));
}, a.prototype.error = function() {
for (var e = [], n = 0; n < arguments.length; n++)
e[n] = arguments[n];
return this.print(new w(i.error, this.name, e));
}, a.prototype.info = function() {
for (var e = [], n = 0; n < arguments.length; n++)
e[n] = arguments[n];
return this.print(new w(i.info, this.name, e));
}, a.prototype.trace = function() {
for (var e = [], n = 0; n < arguments.length; n++)
e[n] = arguments[n];
var c = this.createStack();
return this.print(new w(i.all, this.name, [c].concat(e)));
}, a.prototype.createStack = function() {
var e, n = ((e = new Error().stack) !== null && e !== void 0 ? e : "").replace(`Error
`, ""), c = n.split(`
`);
return c.splice(0, 3), c.join(`
`);
}, a.prototype.print = function(e) {
var n, c, f, m, d = e.logLevel, v = this.options.prepend(e), p = this.options.formatData(e);
return d === i.warn ? (n = this.options.console).warn.apply(n, O(["".concat(v, " ")], p, !1)) : d === i.error ? (c = this.options.console).error.apply(c, O(["".concat(v, " ")], p, !1)) : d === i.info ? (f = this.options.console).info.apply(f, O(["".concat(v, " ")], p, !1)) : (m = this.options.console).log.apply(m, O(["".concat(v, " ")], p, !1));
}, a;
}(), function() {
var a = typeof Symbol == "function" && Symbol.metadata ? /* @__PURE__ */ Object.create(null) : void 0;
l = [_(i.warn)], o = [_(i.all)], u = [_(i.error)], s = [_(i.info)], g = [_(i.all)], b(r, null, l, { kind: "method", name: "warn", static: !1, private: !1, access: { has: function(e) {
return "warn" in e;
}, get: function(e) {
return e.warn;
} }, metadata: a }, null, t), b(r, null, o, { kind: "method", name: "log", static: !1, private: !1, access: { has: function(e) {
return "log" in e;
}, get: function(e) {
return e.log;
} }, metadata: a }, null, t), b(r, null, u, { kind: "method", name: "error", static: !1, private: !1, access: { has: function(e) {
return "error" in e;
}, get: function(e) {
return e.error;
} }, metadata: a }, null, t), b(r, null, s, { kind: "method", name: "info", static: !1, private: !1, access: { has: function(e) {
return "info" in e;
}, get: function(e) {
return e.info;
} }, metadata: a }, null, t), b(r, null, g, { kind: "method", name: "trace", static: !1, private: !1, access: { has: function(e) {
return "trace" in e;
}, get: function(e) {
return e.trace;
} }, metadata: a }, null, t), a && Object.defineProperty(r, Symbol.metadata, { enumerable: !0, configurable: !0, writable: !0, value: a });
}(), r;
}();
export {
w as LogEvent,
i as LogLevel,
k as Logger,
j as QueryKey
};