From 2857ac3e475ec2e45d83a9ef5d46a0a33b8c55dd Mon Sep 17 00:00:00 2001 From: Asger Finding Date: Tue, 8 Aug 2023 14:26:53 +0200 Subject: [PATCH] feat: add margin to logger messages with newline --- src/common/logger.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/common/logger.ts b/src/common/logger.ts index 6272864..435b28b 100644 --- a/src/common/logger.ts +++ b/src/common/logger.ts @@ -11,13 +11,28 @@ const separator = process.platform === 'win32' ? '>' : '›'; const newTimestamp = (): string => new Date().toISOString() .slice(11, -1); +const newlineSpacer = ' '.repeat(newTimestamp().length + separator.length + 2); + +/** + * Takes in log arguments, finds strings + * and adds adequate spacing to newlines + * to stay inline with the first line with + * the timestamp. + * + * @param args Arguments passed to log function + * @returns Transformed argument list + */ +const transformArgs = (args: unknown[]): unknown[] => args.map(arg => (typeof arg === 'string' + ? arg.replaceAll('\n', `\n${ newlineSpacer }`) + : arg)); + /** * If in development, log a timestamped message to the console in blue. * * @param args The arguments to log */ export const info = (...args: unknown[]): void => { - if (IS_DEVELOPMENT) console.log('\x1b[36m%s\x1b[0m', newTimestamp(), separator, ...args); + if (IS_DEVELOPMENT) console.log('\x1b[36m%s\x1b[0m', newTimestamp(), separator, ...transformArgs(args)); }; /** @@ -26,7 +41,7 @@ export const info = (...args: unknown[]): void => { * @param args The arguments to log */ export const warn = (...args: unknown[]): void => { - if (IS_DEVELOPMENT) console.warn('\x1b[33m%s\x1b[0m', newTimestamp(), separator, ...args); + if (IS_DEVELOPMENT) console.warn('\x1b[33m%s\x1b[0m', newTimestamp(), separator, ...transformArgs(args)); }; /** @@ -35,5 +50,5 @@ export const warn = (...args: unknown[]): void => { * @param args The arguments to log */ export const error = (...args: unknown[]): void => { - if (IS_DEVELOPMENT) console.error('\x1b[31m%s\x1b[0m', newTimestamp(), separator, ...args); + if (IS_DEVELOPMENT) console.error('\x1b[31m%s\x1b[0m', newTimestamp(), separator, ...transformArgs(args)); };