Skip to content

Commit

Permalink
Include the caller file name in warn logs
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Boberg <[email protected]>
  • Loading branch information
axelboberg committed Apr 23, 2024
1 parent 0784c55 commit 7287a4b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
78 changes: 60 additions & 18 deletions lib/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<String[]> }
*/
#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
}

/**
Expand All @@ -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`
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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.<String, any> } meta Any additional data to make
Expand Down Expand Up @@ -196,10 +241,7 @@ class Logger {
*/
this.level = level

/**
* @private
*/
this._formatter = formatter
this.#formatter = formatter
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7287a4b

Please sign in to comment.