Skip to content

Commit

Permalink
feat: Support new log event levels (debug and fatal) (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanphamcybozu committed Sep 7, 2023
1 parent dc848fd commit 33c4652
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
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(),
fatal: jest.fn(),
};

describe("ProgressLogger", () => {
Expand Down
41 changes: 41 additions & 0 deletions src/utils/__tests__/log.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
13 changes: 9 additions & 4 deletions src/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
},
};
Expand Down

0 comments on commit 33c4652

Please sign in to comment.