From 33c4652ce10cafd7e5fba6c63b6a19fa22347d98 Mon Sep 17 00:00:00 2001 From: tuanphamcybozu <41720778+tuanphamcybozu@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:18:19 +0700 Subject: [PATCH] feat: Support new log event levels (`debug` and `fatal`) (#473) --- .../usecases/__tests__/add/progress.test.ts | 1 + src/utils/__tests__/log.test.ts | 41 +++++++++++++++++++ src/utils/log.ts | 13 ++++-- 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/utils/__tests__/log.test.ts diff --git a/src/record/import/usecases/__tests__/add/progress.test.ts b/src/record/import/usecases/__tests__/add/progress.test.ts index 84a6995077..53273361a8 100644 --- a/src/record/import/usecases/__tests__/add/progress.test.ts +++ b/src/record/import/usecases/__tests__/add/progress.test.ts @@ -7,6 +7,7 @@ const mockLogger: Logger = { warn: jest.fn(), error: jest.fn(), debug: jest.fn(), + fatal: jest.fn(), }; describe("ProgressLogger", () => { diff --git a/src/utils/__tests__/log.test.ts b/src/utils/__tests__/log.test.ts new file mode 100644 index 0000000000..c147135850 --- /dev/null +++ b/src/utils/__tests__/log.test.ts @@ -0,0 +1,41 @@ +import type { Logger } from "../log"; +import { logger } from "../log"; + +describe("logger", () => { + const mockDate = new Date(0); + const spy = jest.spyOn(global, "Date").mockImplementation(() => mockDate); + + const patternTest = [ + ["DEBUG", "debug"], + ["INFO", "info"], + ["WARN", "warn"], + ["ERROR", "error"], + ["FATAL", "fatal"], + ]; + it.each(patternTest)( + `should show the %s log when calling logger with %s level`, + (logDisplay, logLevel) => { + const consoleMock = jest + .spyOn(console, "error") + .mockImplementation(() => { + return true; + }); + + logger[logLevel as keyof Logger]("This is an example message."); + + expect(consoleMock).toHaveBeenCalledTimes(1); + expect(consoleMock).toHaveBeenCalledWith( + expect.stringMatching( + new RegExp( + `\\[1970-01-01T00:00:00.000Z] (.*)${logDisplay}(.*): This is an example message.`, + ), + ), + ); + }, + ); + + afterAll(() => { + spy.mockReset(); + spy.mockRestore(); + }); +}); diff --git a/src/utils/log.ts b/src/utils/log.ts index ce40d16a97..784aaab115 100644 --- a/src/utils/log.ts +++ b/src/utils/log.ts @@ -4,13 +4,19 @@ import { CliKintoneError } from "./error"; const currentISOString = () => new Date().toISOString(); export type Logger = { + debug: (message: any) => void; info: (message: any) => void; warn: (message: any) => void; error: (message: any) => void; - debug: (message: any) => void; + fatal: (message: any) => void; }; export const logger: Logger = { + debug: (message: any) => { + const prefix = `[${currentISOString()}] ${chalkStderr.green("DEBUG")}:`; + console.error(addPrefixEachLine(message, prefix)); + }, + info: (message: any) => { const prefix = `[${currentISOString()}] ${chalkStderr.blue("INFO")}:`; console.error(addPrefixEachLine(message, prefix)); @@ -27,9 +33,8 @@ export const logger: Logger = { console.error(addPrefixEachLine(parsedMessage, prefix)); }, - debug: (message: any) => { - return; // TODO: Decide how enable debug log - const prefix = `[${currentISOString()}] ${chalkStderr.yellow("DEBUG")}:`; + fatal: (message: any) => { + const prefix = `[${currentISOString()}] ${chalkStderr.bgRed("FATAL")}:`; console.error(addPrefixEachLine(message, prefix)); }, };