Skip to content

Commit

Permalink
Merge pull request #10291 from yukukotani/console-logger-stringify-array
Browse files Browse the repository at this point in the history
fix(common): stringify arrays on printing with console-logger
  • Loading branch information
kamilmysliwiec committed Sep 19, 2022
2 parents 41a4753 + d8cd248 commit 3505a00
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/common/services/console-logger.service.ts
Expand Up @@ -220,7 +220,7 @@ export class ConsoleLogger implements LoggerService {
}

protected stringifyMessage(message: unknown, logLevel: LogLevel) {
return isPlainObject(message)
return isPlainObject(message) || Array.isArray(message)
? `${this.colorize('Object:', logLevel)}\n${JSON.stringify(
message,
(key, value) =>
Expand Down
49 changes: 49 additions & 0 deletions packages/common/test/services/logger.service.spec.ts
Expand Up @@ -576,5 +576,54 @@ describe('Logger', () => {
`Prefix: ~~~test~~~`,
);
});

it('should stringify messages', () => {
class CustomConsoleLogger extends ConsoleLogger {
protected colorize(message: string, _: LogLevel): string {
return message;
}
}

const consoleLogger = new CustomConsoleLogger();
const consoleLoggerSpy = sinon.spy(
consoleLogger,
'stringifyMessage' as keyof ConsoleLogger,
);
consoleLogger.debug(
'str1',
{ key: 'str2' },
['str3'],
[{ key: 'str4' }],
null,
1,
);

expect(consoleLoggerSpy.getCall(0).returnValue).to.equal('str1');
expect(consoleLoggerSpy.getCall(1).returnValue).to.equal(
`Object:
{
"key": "str2"
}
`,
);
expect(consoleLoggerSpy.getCall(2).returnValue).to.equal(
`Object:
[
"str3"
]
`,
);
expect(consoleLoggerSpy.getCall(3).returnValue).to.equal(
`Object:
[
{
"key": "str4"
}
]
`,
);
expect(consoleLoggerSpy.getCall(4).returnValue).to.equal(null);
expect(consoleLoggerSpy.getCall(5).returnValue).to.equal(1);
});
});
});

0 comments on commit 3505a00

Please sign in to comment.