diff --git a/src/common/logger.ts b/src/common/logger.ts index 22fb05f..c01acd1 100644 --- a/src/common/logger.ts +++ b/src/common/logger.ts @@ -1,4 +1,5 @@ import { AsyncLocalStorage } from 'async_hooks' +import dayjs from './dayjs' export enum LogLevel { DEBUG, @@ -42,8 +43,9 @@ export class Logger { break } - let fmtString = '<%s> [%s] %s: %s' + let fmtString = '(%s) <%s> [%s] %s: %s' const params: Array = [ + dayjs().format(), this.tag, this.correlationId, levelString, diff --git a/tests/unit/common/logger.test.ts b/tests/unit/common/logger.test.ts index b5a3e24..55fe83d 100644 --- a/tests/unit/common/logger.test.ts +++ b/tests/unit/common/logger.test.ts @@ -1,5 +1,8 @@ +import dayjs from '../../../src/common/dayjs' import { Logger, LogLevel } from '../../../src/common/logger' +jest.useFakeTimers().setSystemTime(new Date('2023-01-01')) + describe('Logger', () => { beforeEach(() => { jest.resetAllMocks() @@ -19,7 +22,8 @@ describe('Logger', () => { expect(consoleLogSpy).toHaveBeenCalledTimes(2) expect(consoleLogSpy).toHaveBeenNthCalledWith( 1, - '<%s> [%s] %s: %s', + '(%s) <%s> [%s] %s: %s', + dayjs().format(), 'tag', 'abc', 'DEBUG', @@ -27,7 +31,8 @@ describe('Logger', () => { ) expect(consoleLogSpy).toHaveBeenNthCalledWith( 2, - '<%s> [%s] %s: %s', + '(%s) <%s> [%s] %s: %s', + dayjs().format(), 'tag', 'abc', 'INFO', @@ -35,7 +40,8 @@ describe('Logger', () => { ) expect(consoleErrorSpy).toHaveBeenCalledTimes(1) expect(consoleErrorSpy).toHaveBeenCalledWith( - '<%s> [%s] %s: %s - %O', + '(%s) <%s> [%s] %s: %s - %O', + dayjs().format(), 'tag', 'abc', 'ERROR', @@ -66,7 +72,8 @@ describe('Logger', () => { expect(consoleLogSpy).toHaveBeenCalledTimes(1) expect(consoleLogSpy).toHaveBeenNthCalledWith( 1, - '<%s> [%s] %s: %s - %O', + '(%s) <%s> [%s] %s: %s - %O', + dayjs().format(), 'tag', 'abc', 'INFO',