Skip to content

Commit

Permalink
feat: support trace log level (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
hung-cybo committed Jun 14, 2024
1 parent ac07129 commit 5878c89
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/record/import/usecases/__tests__/add/progress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const mockLogger: Logger = {
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
trace: jest.fn(),
fatal: jest.fn(),
};

Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 15 additions & 1 deletion src/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,6 +11,7 @@ export interface Logger {
}

export const LOG_CONFIG_LEVELS = <const>[
"trace",
"debug",
"info",
"warn",
Expand All @@ -20,7 +22,13 @@ export const LOG_CONFIG_LEVELS = <const>[

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;
Expand All @@ -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 });
}
Expand Down Expand Up @@ -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"],
Expand All @@ -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"),
Expand Down

0 comments on commit 5878c89

Please sign in to comment.