From 7287a4b36edcf2e87031224ed9002e978edc6536 Mon Sep 17 00:00:00 2001 From: Axel Boberg Date: Wed, 24 Apr 2024 00:58:54 +0200 Subject: [PATCH] Include the caller file name in warn logs Signed-off-by: Axel Boberg --- lib/Logger.js | 78 +++++++++++++++++++++++++++++++++++++----------- lib/api/index.js | 2 +- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/lib/Logger.js b/lib/Logger.js index 1a8c4cc..4462cf9 100644 --- a/lib/Logger.js +++ b/lib/Logger.js @@ -4,21 +4,33 @@ const DEFAULT_LOG_LEVEL = process.env.LOG_LEVEL || 'debug' -function defaultFormatter (type, logger, ...args) { - return [`${type} [${logger.meta?.name}]`, ...args] +function defaultFormatter (type, logger, ctx, ...args) { + return [`${type} [${logger.meta?.name}]${ctx?.caller ? ` (${ctx.caller})` : ''}`, ...args] } class Logger { + /** + * @private + * @type { Function. } + */ + #formatter + + /** + * @private + * @type { Logger | undefined } + */ + static #instance + /** * Get the singleton instance * of this class * @returns { Logger } */ static getInstance () { - if (!this._instance) { - this._instance = new Logger({ name: 'global' }) + if (!this.#instance) { + this.#instance = new Logger({ name: 'global' }) } - return this._instance + return this.#instance } /** @@ -42,17 +54,17 @@ class Logger { */ static get color () { return Object.freeze({ - red: str => Logger._color(str, '\x1b[31m'), - yellow: str => Logger._color(str, '\x1b[33m'), - blue: str => Logger._color(str, '\x1b[34m'), - cyan: str => Logger._color(str, '\x1b[36m') + red: str => Logger.#color(str, '\x1b[31m'), + yellow: str => Logger.#color(str, '\x1b[33m'), + blue: str => Logger.#color(str, '\x1b[34m'), + cyan: str => Logger.#color(str, '\x1b[36m') }) } /** * @private */ - static _color (str, color) { + static #color (str, color) { return `${color}${str}\x1b[0m` } @@ -123,7 +135,7 @@ class Logger { */ debug (...args) { if (this.level > Logger.levels.debug) return - const formatted = this._formatter(Logger.color.blue('debug'), this, ...args) + const formatted = this.#formatter(Logger.color.blue('debug'), this, {}, ...args) console.log(...formatted) } @@ -133,7 +145,7 @@ class Logger { */ info (...args) { if (this.level > Logger.levels.info) return - const formatted = this._formatter(Logger.color.cyan('info'), this, ...args) + const formatted = this.#formatter(Logger.color.cyan('info'), this, {}, ...args) console.info(...formatted) } @@ -143,7 +155,7 @@ class Logger { */ warn (...args) { if (this.level > Logger.levels.warn) return - const formatted = this._formatter(Logger.color.yellow('warn'), this, ...args) + const formatted = this.#formatter(Logger.color.yellow('warn'), this, { caller: this.#getCallerFile() }, ...args) console.warn(...formatted) } @@ -153,7 +165,7 @@ class Logger { */ error (...args) { if (this.level > Logger.levels.error) return - const formatted = this._formatter(Logger.color.red('error'), this, ...args) + const formatted = this.#formatter(Logger.color.red('error'), this, {}, ...args) console.error(...formatted) } @@ -166,6 +178,39 @@ class Logger { console.log(...args) } + /** + * @private + * Get the caller file + * of the function + * + * This is useful for debugging + * + * @returns { String } + */ + #getCallerFile () { + const original = Error.prepareStackTrace + let callerFile + + try { + const err = new Error() + Error.prepareStackTrace = function (_, stack) { + return stack + } + + const currentFile = err.stack.shift().getFileName() + + while (err.stack.length) { + callerFile = err.stack.shift().getFileName() + if (currentFile !== callerFile) { + break + } + } + } catch (_) {} + + Error.prepareStackTrace = original + return callerFile + } + /** * Create a new logger * @param { Object. } meta Any additional data to make @@ -196,10 +241,7 @@ class Logger { */ this.level = level - /** - * @private - */ - this._formatter = formatter + this.#formatter = formatter } } diff --git a/lib/api/index.js b/lib/api/index.js index f72a791..120a642 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -114,7 +114,7 @@ function factory (workspace) { const handler = handlers.get(command) if (!handler) { - logger.debug('No such command', command) + logger.warn('No such command', command) return } handler.call(...args)