From 5878c896d8665d39ad3c11d994052aae0fafa5e4 Mon Sep 17 00:00:00 2001 From: Nguyen Thai Hung <59815499+hung-cybo@users.noreply.github.com> Date: Fri, 14 Jun 2024 15:05:50 +0700 Subject: [PATCH] feat: support trace log level (#787) --- .../usecases/__tests__/add/progress.test.ts | 1 + src/utils/__tests__/log.test.ts | 2 ++ src/utils/log.ts | 16 +++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/record/import/usecases/__tests__/add/progress.test.ts b/src/record/import/usecases/__tests__/add/progress.test.ts index 53273361a8..bc1e4c530f 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(), + trace: jest.fn(), fatal: jest.fn(), }; diff --git a/src/utils/__tests__/log.test.ts b/src/utils/__tests__/log.test.ts index 40df08db16..89d1afd479 100644 --- a/src/utils/__tests__/log.test.ts +++ b/src/utils/__tests__/log.test.ts @@ -5,6 +5,7 @@ describe("StandardLogger", () => { const mockDate = new Date(0); const spy = jest.spyOn(global, "Date").mockImplementation(() => mockDate); const patternTest = [ + ["TRACE", "trace"], ["DEBUG", "debug"], ["INFO", "info"], ["WARN", "warn"], @@ -75,6 +76,7 @@ describe("StandardLogger", () => { const standardLogger = new StandardLogger(options); const formatSpy = jest.spyOn(standardLogger as any, "format"); standardLogger.setLogConfigLevel("warn"); + standardLogger.trace(message); standardLogger.debug(message); standardLogger.info(message); standardLogger.warn(message); diff --git a/src/utils/log.ts b/src/utils/log.ts index f3415d9c41..c5a8ed9d14 100644 --- a/src/utils/log.ts +++ b/src/utils/log.ts @@ -2,6 +2,7 @@ import { stderr as chalkStderr } from "chalk"; import { CliKintoneError } from "./error"; export interface Logger { + trace: (message: any) => void; debug: (message: any) => void; info: (message: any) => void; warn: (message: any) => void; @@ -10,6 +11,7 @@ export interface Logger { } export const LOG_CONFIG_LEVELS = [ + "trace", "debug", "info", "warn", @@ -20,7 +22,13 @@ export const LOG_CONFIG_LEVELS = [ export type LogConfigLevel = (typeof LOG_CONFIG_LEVELS)[number]; -export type LogEventLevel = "debug" | "info" | "warn" | "error" | "fatal"; +export type LogEventLevel = + | "trace" + | "debug" + | "info" + | "warn" + | "error" + | "fatal"; export type LogEvent = { level: LogEventLevel; @@ -46,6 +54,10 @@ export class StandardLogger implements Logger { } } + trace(message: any): void { + this.log({ level: "trace", message }); + } + debug(message: any): void { this.log({ level: "debug", message }); } @@ -79,6 +91,7 @@ export class StandardLogger implements Logger { const logConfigLevelMatrix: { [configLevel in LogConfigLevel]: LogEventLevel[]; } = { + trace: ["trace", "debug", "info", "warn", "error", "fatal"], debug: ["debug", "info", "warn", "error", "fatal"], info: ["info", "warn", "error", "fatal"], warn: ["warn", "error", "fatal"], @@ -93,6 +106,7 @@ export class StandardLogger implements Logger { private format(event: LogEvent): string { const timestamp = new Date().toISOString(); const eventLevelLabels: { [level in LogEventLevel]: string } = { + trace: chalkStderr.bgGreen("TRACE"), debug: chalkStderr.green("DEBUG"), info: chalkStderr.blue("INFO"), warn: chalkStderr.yellow("WARN"),