UNPKG

@xylabs/logger

Version:

XYLabs Logger Library

91 lines (89 loc) 2.1 kB
// src/getFunctionName.ts import { handleError } from "@xylabs/error"; var getFunctionName = (depth = 2) => { try { throw new Error("Getting function name"); } catch (ex) { return handleError(ex, (error) => { let newIndex; const stackParts = error.stack?.split("\n")[depth]?.split(" "); const funcName = stackParts?.find((item, index) => { if (item.length > 0 && item !== "at") { if (item === "new") { newIndex = index; } return item; } }) ?? "<unknown>"; return newIndex ? `${funcName} ${stackParts?.[newIndex + 1]}` : funcName; }); } }; // src/IdLogger.ts var IdLogger = class { _id; _logger; constructor(logger, id) { this._logger = logger; this._id = id; } set id(id) { this._id = () => id; } debug(...data) { this._logger?.debug(this.prefix(), ...data); } error(...data) { this._logger?.error(this.prefix(), ...data); } info(...data) { this._logger?.info(this.prefix(), ...data); } log(...data) { this._logger?.log(this.prefix(), ...data); } warn(...data) { this._logger?.warn(this.prefix(), ...data); } prefix() { return `[${this._id?.()}]`; } }; // src/Logger.ts import { Enum } from "@xylabs/enum"; var LogLevel = Enum({ error: 1, warn: 2, info: 3, log: 4, debug: 5 }); var NoOpLogFunction = (..._data) => void {}; var ConsoleLogger = class { constructor(level = LogLevel.warn) { this.level = level; } get debug() { return this.level >= LogLevel.debug ? console.debug : NoOpLogFunction; } get error() { return this.level >= LogLevel.error ? console.error : NoOpLogFunction; } get info() { return this.level >= LogLevel.info ? console.info : NoOpLogFunction; } get log() { return this.level >= LogLevel.log ? console.log : NoOpLogFunction; } get warn() { return this.level >= LogLevel.warn ? console.warn : NoOpLogFunction; } }; export { ConsoleLogger, IdLogger, LogLevel, NoOpLogFunction, getFunctionName }; //# sourceMappingURL=index.mjs.map