@roarr/browser-log-writer
Version:
Roarr log writer for use in a web browser.
131 lines (130 loc) • 5.32 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLogWriter = void 0;
var createLogMethods_1 = require("./createLogMethods");
var boolean_1 = require("boolean");
var globalthis_1 = __importDefault(require("globalthis"));
var liqe_1 = require("liqe");
var roarr_1 = require("roarr");
var globalThis = (0, globalthis_1.default)();
var logLevelColors = {
debug: {
backgroundColor: '#712bde',
color: '#fff',
},
error: {
backgroundColor: '#f05033',
color: '#fff',
},
fatal: {
backgroundColor: '#f05033',
color: '#fff',
},
info: {
backgroundColor: '#3174f1',
color: '#fff',
},
trace: {
backgroundColor: '#666',
color: '#fff',
},
warn: {
backgroundColor: '#f5a623',
color: '#000',
},
};
var namespaceColors = {
debug: {
color: '#8367d3',
},
error: {
color: '#ff1a1a',
},
fatal: {
color: '#ff1a1a',
},
info: {
color: '#3291ff',
},
trace: {
color: '#999',
},
warn: {
color: '#f7b955',
},
};
var findLiqeQuery = function (storage) {
var query = storage.getItem('ROARR_FILTER');
return query ? (0, liqe_1.parse)(query) : null;
};
var createLogWriter = function (configuration) {
var _a, _b, _c;
if (configuration === void 0) { configuration = {}; }
var styleOutput = (_a = configuration === null || configuration === void 0 ? void 0 : configuration.styleOutput) !== null && _a !== void 0 ? _a : true;
var storage = (_b = configuration === null || configuration === void 0 ? void 0 : configuration.storage) !== null && _b !== void 0 ? _b : globalThis.localStorage;
var logMethods = (_c = configuration === null || configuration === void 0 ? void 0 : configuration.logMethods) !== null && _c !== void 0 ? _c : (0, createLogMethods_1.createLogMethods)();
if (!storage && !globalThis.localStorage) {
// eslint-disable-next-line no-console
console.warn('initiated Roarr browser log writer in non-browser context');
return function () {
// Do nothing.
};
}
if (!(0, boolean_1.boolean)(storage.getItem('ROARR_LOG'))) {
return function () {
// Do nothing.
};
}
var liqeQuery = findLiqeQuery(storage);
if (styleOutput) {
return function (message) {
var payload = JSON.parse(message);
var _a = payload.context, numericLogLevel = _a.logLevel, namespace = _a.namespace, context = __rest(_a, ["logLevel", "namespace"]);
if (liqeQuery && !(0, liqe_1.test)(liqeQuery, payload)) {
return;
}
var logLevelName = (0, roarr_1.getLogLevelName)(Number(numericLogLevel));
var logMethod = logMethods[logLevelName];
var logColor = logLevelColors[logLevelName];
var styles = "\n background-color: ".concat(logColor.backgroundColor, ";\n color: ").concat(logColor.color, ";\n font-weight: bold;\n ");
var namespaceStyles = "\n color: ".concat(namespaceColors[logLevelName].color, ";\n ");
var resetStyles = "\n color: inherit;\n ";
if (Object.keys(context).length > 0) {
logMethod("%c ".concat(logLevelName, " %c").concat(namespace ? " [".concat(String(namespace), "]:") : '', "%c ").concat(payload.message, " %O"), styles, namespaceStyles, resetStyles, context);
}
else {
logMethod("%c ".concat(logLevelName, " %c").concat(namespace ? " [".concat(String(namespace), "]:") : '', "%c ").concat(payload.message), styles, namespaceStyles, resetStyles);
}
};
}
return function (message) {
var payload = JSON.parse(message);
var _a = payload.context, numericLogLevel = _a.logLevel, namespace = _a.namespace, context = __rest(_a, ["logLevel", "namespace"]);
if (liqeQuery && !(0, liqe_1.test)(liqeQuery, payload)) {
return;
}
var logLevelName = (0, roarr_1.getLogLevelName)(Number(numericLogLevel));
var logMethod = logMethods[logLevelName];
if (Object.keys(context).length > 0) {
logMethod("".concat(logLevelName, " ").concat(namespace ? " [".concat(String(namespace), "]:") : '', " ").concat(payload.message), context);
}
else {
logMethod("".concat(logLevelName, " ").concat(namespace ? " [".concat(String(namespace), "]:") : '', " ").concat(payload.message));
}
};
};
exports.createLogWriter = createLogWriter;