From 2e31aa1ee1dfe4efdee8a7866c366d62fcabad16 Mon Sep 17 00:00:00 2001 From: Moustafaa91 Date: Mon, 4 Nov 2024 05:09:38 +0000 Subject: [PATCH 1/2] Add features passing logger name and createChild logger name --- packages/logger/src/index.ts | 51 ++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.ts index 63c5bb8c62..958ced1847 100644 --- a/packages/logger/src/index.ts +++ b/packages/logger/src/index.ts @@ -7,16 +7,24 @@ const ansiCodes = { magenta: '\x1b[35m', cyan: '\x1b[36m', reset: '\x1b[0m', + bold: '\x1b[1m', } as const; export const warnPrefix = ansiCodes.yellow + 'WARN' + ansiCodes.reset; export const infoPrefix = ansiCodes.cyan + 'INFO' + ansiCodes.reset; export const errorPrefix = ansiCodes.red + 'ERR' + ansiCodes.reset; export const debugPrefix = ansiCodes.magenta + 'DEBUG' + ansiCodes.reset; +export const titleBold = (msg: string) => ansiCodes.bold + msg + ansiCodes.reset; export type LogLevel = 'debug' | 'info' | 'warn' | 'error'; -export type YogaLogger = Record void>; +export type YogaLogger = { + debug: (...args: any[]) => void; + info: (...args: any[]) => void; + warn: (...args: any[]) => void; + error: (...args: any[]) => void; + createChild: (prefix: string) => YogaLogger; +}; const logLevelScores: Record = { debug: 0, @@ -29,32 +37,29 @@ const logLevelScores: Record = { // eslint-disable-next-line @typescript-eslint/no-empty-function const noop = () => {}; -const consoleLog = - (prefix: string) => - (...args: Array) => - console.log(prefix, ...args); - -const debugLog = console.debug - ? (...args: Array) => console.debug(debugPrefix, ...args) - : consoleLog(debugPrefix); -const infoLog = console.info - ? (...args: Array) => console.info(infoPrefix, ...args) - : consoleLog(infoPrefix); -const warnLog = console.warn - ? (...args: Array) => console.warn(warnPrefix, ...args) - : consoleLog(warnPrefix); -const errorLog = console.error - ? (...args: Array) => console.error(errorPrefix, ...args) - : consoleLog(errorPrefix); +const withPrefix = + (prefix: string, name?: string) => + (...args: Array) => { + const namePrefix = name ? `[${titleBold(name)}] ` + ' ' : ''; + console.log(namePrefix + prefix, ...args); + }; + +const createLogFunction = (prefix: string, name?: string) => withPrefix(prefix, name); export const createLogger = ( logLevel: LogLevel | 'silent' = globalThis.process?.env['DEBUG'] === '1' ? 'debug' : 'info', + name?: string, ): YogaLogger => { const score = logLevelScores[logLevel]; - return { - debug: score > logLevelScores.debug ? noop : debugLog, - info: score > logLevelScores.info ? noop : infoLog, - warn: score > logLevelScores.warn ? noop : warnLog, - error: score > logLevelScores.error ? noop : errorLog, + + const logger: YogaLogger = { + debug: score > logLevelScores.debug ? noop : createLogFunction(debugPrefix, name), + info: score > logLevelScores.info ? noop : createLogFunction(infoPrefix, name), + warn: score > logLevelScores.warn ? noop : createLogFunction(warnPrefix, name), + error: score > logLevelScores.error ? noop : createLogFunction(errorPrefix, name), + createChild: (childPrefix: string) => + createLogger(logLevel, String(name ? `${name}]-[${childPrefix}` : '')), }; + + return logger; }; From 3ccd91f24d39ede230dbee594f848613697aa690 Mon Sep 17 00:00:00 2001 From: Moustafaa91 Date: Tue, 5 Nov 2024 00:18:34 +0000 Subject: [PATCH 2/2] Make createChild as optional --- packages/logger/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.ts index 958ced1847..6013aceda7 100644 --- a/packages/logger/src/index.ts +++ b/packages/logger/src/index.ts @@ -23,7 +23,7 @@ export type YogaLogger = { info: (...args: any[]) => void; warn: (...args: any[]) => void; error: (...args: any[]) => void; - createChild: (prefix: string) => YogaLogger; + createChild?: (prefix: string) => YogaLogger; }; const logLevelScores: Record = {