UNPKG

sdj-esm

Version:
137 lines (136 loc) 4.42 kB
/* Copyright (c) 2023-2024 Will Rudolph <@willrudolph.com> This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import { ESDJ_LOG } from "../core/statics.js"; import { find, isArray } from "lodash-es"; const ECODES = [ "CRIT ", // 0 - Will throw Error in PROD "Error", // 1 - Logs in DEV/LIB / No error in Prod "Warn ", // 2 - Logs in DEV/LIB / No error in Prod "Lib. ", // 3 - "Debug", // 4 "Trace", // 5 ], ECODE_COLORS = [ "font-weight: bold; color: #FF0000;", "font-weight: bold; color: #FF9900;", "font-weight: bold; color: #AAAA00;", "font-weight: bold; color: #AAFFAA;", "font-weight: bold; color: #FFFFFF;", "font-weight: normal; color: #FFFFFF;" ]; export class Logger { store; fileName; constructor(name) { const lastIdx = (name.lastIndexOf("\\") === -1) ? 0 : name.lastIndexOf("\\") + 1; this.fileName = name.substring(lastIdx); } makeString(logDet) { let asString = String(logDet.time), tBuild; while (asString.length < 6) { asString = "0" + asString; } tBuild = asString + "| " + ECODES[logDet.level]; tBuild += " [" + logDet.fromModule + "]:"; tBuild += logDet.message; return tBuild; } toConsole(logDet) { // eslint-disable-next-line no-console console.log("%c " + this.makeString(logDet), ECODE_COLORS[logDet.level]); } } export class LogManager { storeLvl = 0; maxLogs = 0; consoleLvl = 0; _startTime; _loggerStore; _logMode; constructor(logMode, store = undefined) { this._loggerStore = (store && isArray(store)) ? store : []; this._startTime = Date.now(); this.setLogMode(logMode); this._logMode = logMode; } set logMode(inMode) { this.setLogMode(inMode); } get logMode() { return this._logMode; } getLogFunc(name) { let existingLogger = find(this._loggerStore, { fileName: name }); if (!existingLogger) { existingLogger = new Logger(name); this._loggerStore.push(existingLogger); } return (msg, dbLevel) => { if (!msg || msg === "" || (this.storeLvl === 0 && this.consoleLvl === 0)) { return; } if (existingLogger) { const logDetail = { message: msg, time: this.getTime(), fromModule: existingLogger.fileName, level: (dbLevel) ? dbLevel : ECODES.length - 1 }; if (this.consoleLvl <= logDetail.level) { existingLogger.toConsole(logDetail); } if (this.storeLvl <= logDetail.level) { if (!existingLogger.store) { existingLogger.store = []; } existingLogger.store.push(logDetail); if (this.maxLogs !== -1 && existingLogger.store.length > this.maxLogs) { existingLogger.store.shift(); } } } }; } getTime() { const now = Date.now(); return now - this._startTime; } setLogMode(setMode) { switch (setMode) { case ESDJ_LOG.DEV: this.storeLvl = 5; this.maxLogs = -1; this.consoleLvl = 5; break; case ESDJ_LOG.LIB: this.storeLvl = 3; this.maxLogs = 10; this.consoleLvl = 3; break; case ESDJ_LOG.TEST: this.storeLvl = 5; this.maxLogs = 5; this.consoleLvl = 5; break; case ESDJ_LOG.PROD: this.storeLvl = 0; this.maxLogs = 0; this.consoleLvl = 0; break; case ESDJ_LOG.RUN: this.storeLvl = 4; this.maxLogs = 20; this.consoleLvl = 0; break; default: // no default } this._logMode = setMode; } } //# sourceMappingURL=log.js.map