UNPKG

@dvcol/common-utils

Version:

Typescript library for common utility functions and constants

204 lines (202 loc) 7.39 kB
// lib/common/utils/logger.utils.ts var LogLevel = /* @__PURE__ */ ((LogLevel2) => { LogLevel2[LogLevel2["Silent"] = -1] = "Silent"; LogLevel2[LogLevel2["Error"] = 0] = "Error"; LogLevel2[LogLevel2["Warn"] = 1] = "Warn"; LogLevel2[LogLevel2["Info"] = 2] = "Info"; LogLevel2[LogLevel2["Debug"] = 3] = "Debug"; LogLevel2[LogLevel2["Trace"] = 4] = "Trace"; return LogLevel2; })(LogLevel || {}); var toLogLevel = (logLevel) => { const _logLevel = logLevel.charAt(0).toUpperCase() + logLevel.slice(1).toLowerCase(); return LogLevel[_logLevel] ?? -1 /* Silent */; }; var TimeStampFormat = { ISO: "ISO", Date: "Date", Time: "Time", Local: "Local" }; var getTimestamp = (scope, format = TimeStampFormat.Time) => { let date; if (format === TimeStampFormat.ISO) date = (/* @__PURE__ */ new Date()).toISOString(); else if (format === TimeStampFormat.Date) date = (/* @__PURE__ */ new Date()).toLocaleDateString().split("T").at(0); else if (format === TimeStampFormat.Local) date = (/* @__PURE__ */ new Date()).toLocaleString(); else date = (/* @__PURE__ */ new Date()).toLocaleTimeString("en", { hour12: false, timeZone: "UTC" }); return scope ? `[${date} - ${scope}]` : `[${date}]`; }; var LoggerColor = { Debug: "#9e9e9e", Info: "#5bd4f9", Warn: "#f2c61a", Error: "#ff5549", Success: "#00ff00" }; var ConsoleFormat = { /** * Formats the value as a string * @see ConsoleFormat */ String: "%s", /** * Formats the value as an integer * @see ConsoleFormat */ Integer: "%i", /** * Formats the value as a floating point value * @see ConsoleFormat */ Float: "%f", /** * Formats the value as an expandable DOM element * @see ConsoleFormat */ DOM: "%o", /** * Formats the value as an expandable JavaScript object * @see ConsoleFormat */ Object: "%O", /** * Applies CSS style rules to the output string as specified by the second parameter * @see ConsoleFormat */ Style: "%c" }; var colorize = (color, ...args) => { if (!args?.some((arg) => typeof arg === "string")) return args; let prefix = "%c"; const _args = [`color: ${color}`]; args.forEach((arg, i) => { const space = i ? " " : ""; if (arg instanceof Element) { if (i < args.length - 1) prefix += `${space}${ConsoleFormat.DOM}`; } else if (typeof arg === "object") { if (i < args.length - 1) prefix += `${space}${ConsoleFormat.Object}`; } else if (typeof arg === "string") { prefix += `${space}${ConsoleFormat.String}`; } else if (typeof arg === "number") { prefix += `${space}${ConsoleFormat.Float}`; } _args.push(arg); }); _args.unshift(prefix); return _args; }; var proxyLoggerFilter = { trace: (logLevel) => logLevel >= 4 /* Trace */, debug: (logLevel) => logLevel >= 3 /* Debug */, info: (logLevel) => logLevel >= 2 /* Info */, warn: (logLevel) => logLevel >= 1 /* Warn */, error: (logLevel) => logLevel >= 0 /* Error */ }; var ProxyLogger = class _ProxyLogger { _logger; _proxy; _timeFormat; _debug; _logLevel; constructor({ logger = console, debug = false, proxy = proxyLoggerFilter, logLevel = 1 /* Warn */, timeFormat = TimeStampFormat.Time } = {}) { this._logger = logger; this._proxy = proxy; this._debug = debug; this._logLevel = logLevel; this._timeFormat = timeFormat; } get timeFormat() { return typeof this._timeFormat === "function" ? this._timeFormat() : this._timeFormat; } get isDebug() { return typeof this._debug === "function" ? this._debug() : this._debug; } get logLevel() { return typeof this._logLevel === "function" ? this._logLevel() : this._logLevel; } set logLevel(level) { if (this._debug) console.debug("[ProxyLogger] - Log level changed:", LogLevel[level]); if (typeof this._logLevel === "function") throw new Error("Cannot set log level when logLevel is a function"); this._logLevel = level; } null = (...args) => { if (this.isDebug) console.debug("[ProxyLogger] - Log suppressed:", ...args); }; get trace() { if (!this._proxy?.trace?.(this.logLevel)) return this.null; return this._logger.trace.bind(this._logger); } get debug() { if (!this._proxy?.debug?.(this.logLevel)) return this.null; return this._logger.debug.bind(this._logger); } get info() { if (!this._proxy?.info?.(this.logLevel)) return this.null; return this._logger.info.bind(this._logger); } get warn() { if (!this._proxy?.warn?.(this.logLevel)) return this.null; return this._logger.warn.bind(this._logger); } get error() { if (!this._proxy?.error?.(this.logLevel)) return this.null; return this._logger.error.bind(this._logger); } static logger = new _ProxyLogger(); static timestamp = getTimestamp; static colorize = colorize; color = { debug: (...args) => this.debug(..._ProxyLogger.colorize(LoggerColor.Debug, ...args)), info: (...args) => this.info(..._ProxyLogger.colorize(LoggerColor.Info, ...args)), warn: (...args) => this.warn(..._ProxyLogger.colorize(LoggerColor.Warn, ...args)), error: (...args) => this.error(..._ProxyLogger.colorize(LoggerColor.Error, ...args)), success: (...args) => this.info(..._ProxyLogger.colorize(LoggerColor.Success, ...args)) }; /** * Logger with timestamp but looses stack trace origin */ time = { debug: (...args) => this.debug(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), info: (...args) => this.info(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), warn: (...args) => this.warn(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), error: (...args) => this.error(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), color: { debug: (...args) => this.color.debug(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), info: (...args) => this.color.info(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), warn: (...args) => this.color.warn(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), error: (...args) => this.color.error(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), success: (...args) => this.color.success(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args) } }; scope = (_scope) => ({ debug: (...args) => this.debug(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), info: (...args) => this.info(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), warn: (...args) => this.warn(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), error: (...args) => this.error(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), color: { debug: (...args) => this.color.debug(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), info: (...args) => this.color.info(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), warn: (...args) => this.color.warn(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), error: (...args) => this.color.error(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), success: (...args) => this.color.success(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args) } }); colored = {}; }; export { LogLevel, toLogLevel, TimeStampFormat, getTimestamp, LoggerColor, ConsoleFormat, colorize, proxyLoggerFilter, ProxyLogger };