Skip to content

Commit

Permalink
All: Improves formatting of log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed May 1, 2024
1 parent 8ec233f commit aac8d58
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
16 changes: 16 additions & 0 deletions packages/utils/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ describe('Logger', () => {
jest.useRealTimers();
});

test.each([
[['one', 'two'], 'one two'],
[[true, false, undefined, null], '<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'));

Expand Down
32 changes: 21 additions & 11 deletions packages/utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<undefined>';
} else if (object === null) {
output = '<null>';
} else if (object === true) {
output = '<true>';
} else if (object === false) {
output = '<false>';
} 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;
Expand All @@ -190,7 +206,7 @@ class Logger {
output = JSON.stringify(object);
}
} else {
output = object;
output = object.toString();
}

return output;
Expand All @@ -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() {
Expand Down

0 comments on commit aac8d58

Please sign in to comment.