From aac8d583729e796c23366972f8dcf71480df01ce Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Wed, 1 May 2024 10:36:20 +0100 Subject: [PATCH] All: Improves formatting of log statements --- packages/utils/Logger.test.ts | 16 ++++++++++++++++ packages/utils/Logger.ts | 32 +++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/packages/utils/Logger.test.ts b/packages/utils/Logger.test.ts index 5b4e50194..34d10752b 100644 --- a/packages/utils/Logger.test.ts +++ b/packages/utils/Logger.test.ts @@ -57,6 +57,22 @@ describe('Logger', () => { jest.useRealTimers(); }); + test.each([ + [['one', 'two'], 'one two'], + [[true, false, undefined, null], ' '], + [['123', 123], '123 123'], + [[['a', 'b', ['sub1', 'sub2']]], '[a, b, [sub1, sub2]]'], + [[''], ''], + [[{ that: 'is json', sub: { key1: 'abc', key2: 'def' } }], '{"that":"is json","sub":{"key1":"abc","key2":"def"}}'], + ])('should format messages correctly', async (input, expected) => { + jest.useFakeTimers().setSystemTime(new Date('2020-01-01')); + const logger = createLogger(); + logger.info(...input); + await logger.waitForFileWritesToComplete_(); + expect(await getLogContent()).toBe(`2020-01-01 00:00:00: testing: ${expected}\n`); + jest.useRealTimers(); + }); + // it('should keep the last lines', async () => { // jest.useFakeTimers().setSystemTime(new Date('2020-01-01')); diff --git a/packages/utils/Logger.ts b/packages/utils/Logger.ts index ce1ba2669..cb80bea32 100644 --- a/packages/utils/Logger.ts +++ b/packages/utils/Logger.ts @@ -177,7 +177,23 @@ class Logger { public objectToString(object: any) { let output = ''; - if (typeof object === 'object') { + if (Array.isArray(object)) { + const serialized: string[] = []; + for (const e of object) { + serialized.push(this.objectToString(e)); + } + output = `[${serialized.join(', ')}]`; + } else if (typeof object === 'string') { + output = object; + } else if (object === undefined) { + output = ''; + } else if (object === null) { + output = ''; + } else if (object === true) { + output = ''; + } else if (object === false) { + output = ''; + } else if (typeof object === 'object') { if (object instanceof Error) { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied object = object as any; @@ -190,7 +206,7 @@ class Logger { output = JSON.stringify(object); } } else { - output = object; + output = object.toString(); } return output; @@ -199,16 +215,10 @@ class Logger { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied public objectsToString(...object: any[]) { const output = []; - if (object.length === 1) { - // Quoting when there is only one argument can make the log more difficult to read, - // particularly when formatting is handled elsewhere. - output.push(this.objectToString(object[0])); - } else { - for (let i = 0; i < object.length; i++) { - output.push(`"${this.objectToString(object[i])}"`); - } + for (let i = 0; i < object.length; i++) { + output.push(this.objectToString(object[i])); } - return output.join(', '); + return output.join(' '); } public static databaseCreateTableSql() {