fix(logger): properly stringify objects for logging (#6384)

* fix(logger): properly stringify objects for logging

* Add tests
This commit is contained in:
Joshua Chen 2022-01-17 20:46:38 +08:00 committed by GitHub
parent cb747025e8
commit a9810db1cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 9 deletions

View file

@ -71,3 +71,107 @@ describe('interpolate', () => {
);
});
});
describe('info', () => {
const consoleMock = jest.spyOn(console, 'info').mockImplementation(() => {});
test('should print objects', () => {
logger.info({a: 1});
logger.info(undefined);
logger.info([1, 2, 3]);
logger.info(new Date(2021, 10, 13));
expect(consoleMock.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"[INFO] {\\"a\\":1}",
],
Array [
"[INFO] undefined",
],
Array [
"[INFO] 1,2,3",
],
Array [
"[INFO] Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)",
],
]
`);
});
});
describe('warn', () => {
const consoleMock = jest.spyOn(console, 'warn').mockImplementation(() => {});
test('should print objects', () => {
logger.warn({a: 1});
logger.warn(undefined);
logger.warn([1, 2, 3]);
logger.warn(new Date(2021, 10, 13));
expect(consoleMock.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"[WARNING] {\\"a\\":1}",
],
Array [
"[WARNING] undefined",
],
Array [
"[WARNING] 1,2,3",
],
Array [
"[WARNING] Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)",
],
]
`);
});
});
describe('error', () => {
const consoleMock = jest.spyOn(console, 'error').mockImplementation(() => {});
test('should print objects', () => {
logger.error({a: 1});
logger.error(undefined);
logger.error([1, 2, 3]);
logger.error(new Date(2021, 10, 13));
expect(consoleMock.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"[ERROR] {\\"a\\":1}",
],
Array [
"[ERROR] undefined",
],
Array [
"[ERROR] 1,2,3",
],
Array [
"[ERROR] Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)",
],
]
`);
});
});
describe('success', () => {
const consoleMock = jest.spyOn(console, 'log').mockImplementation(() => {});
test('should print objects', () => {
logger.success({a: 1});
logger.success(undefined);
logger.success([1, 2, 3]);
logger.success(new Date(2021, 10, 13));
expect(consoleMock.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"[SUCCESS] {\\"a\\":1}",
],
Array [
"[SUCCESS] undefined",
],
Array [
"[SUCCESS] 1,2,3",
],
Array [
"[SUCCESS] Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)",
],
]
`);
});
});

View file

@ -52,6 +52,13 @@ function interpolate(
return res;
}
function stringify(msg: unknown): string {
if (String(msg) === '[object Object]') {
return JSON.stringify(msg);
}
return String(msg);
}
function info(msg: unknown): void;
function info(
msg: TemplateStringsArray,
@ -61,7 +68,7 @@ function info(msg: unknown, ...values: InterpolatableValue[]): void {
console.info(
`${chalk.cyan(chalk.bold('[INFO]'))} ${
values.length === 0
? msg
? stringify(msg)
: interpolate(msg as TemplateStringsArray, ...values)
}`,
);
@ -76,7 +83,7 @@ function warn(msg: unknown, ...values: InterpolatableValue[]): void {
chalk.yellow(
`${chalk.bold('[WARNING]')} ${
values.length === 0
? msg
? stringify(msg)
: interpolate(msg as TemplateStringsArray, ...values)
}`,
),
@ -92,7 +99,7 @@ function error(msg: unknown, ...values: InterpolatableValue[]): void {
chalk.red(
`${chalk.bold('[ERROR]')} ${
values.length === 0
? msg
? stringify(msg)
: interpolate(msg as TemplateStringsArray, ...values)
}`,
),
@ -107,7 +114,7 @@ function success(msg: unknown, ...values: InterpolatableValue[]): void {
console.log(
`${chalk.green(chalk.bold('[SUCCESS]'))} ${
values.length === 0
? msg
? stringify(msg)
: interpolate(msg as TemplateStringsArray, ...values)
}`,
);

View file

@ -321,11 +321,15 @@ describe('mapAsyncSequential', () => {
const timeTotal = timeAfter - timeBefore;
const totalTimeouts = sum(Object.values(itemToTimeout));
expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 5);
expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 20);
expect(itemMapStartsAt['1']).toBeGreaterThanOrEqual(0);
expect(itemMapStartsAt['2']).toBeGreaterThanOrEqual(itemMapEndsAt['1'] - 5);
expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual(itemMapEndsAt['2'] - 5);
expect(itemMapStartsAt['2']).toBeGreaterThanOrEqual(
itemMapEndsAt['1'] - 20,
);
expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual(
itemMapEndsAt['2'] - 20,
);
});
});
@ -353,8 +357,8 @@ describe('findAsyncSequential', () => {
expect(findFn).toHaveBeenNthCalledWith(2, '2');
const timeTotal = timeAfter - timeBefore;
expect(timeTotal).toBeGreaterThanOrEqual(95);
expect(timeTotal).toBeLessThan(105);
expect(timeTotal).toBeGreaterThanOrEqual(80);
expect(timeTotal).toBeLessThan(120);
});
});