From d318fe1d293afd3630b97c984362ea10a1998380 Mon Sep 17 00:00:00 2001 From: Vlad Hetman Date: Thu, 19 Jan 2023 13:56:55 +0200 Subject: [PATCH 1/2] update deps and handle args --- package.json | 14 +++++++------- src/logger.ts | 25 +++++++++++++++++++++---- tests/main.test.ts | 18 +++++++++++++++++- tests/stub.ts | 41 +++++++++++++++++++++++++++++++---------- 4 files changed, 76 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 5c2def2..20132c1 100644 --- a/package.json +++ b/package.json @@ -17,17 +17,17 @@ "tslib": "^2.3.0" }, "devDependencies": { - "@types/jest": "^26.0.23", - "@types/node": "^15.14.0", - "@typescript-eslint/eslint-plugin": "^4.28.1", - "@typescript-eslint/parser": "^4.28.1", - "eslint": "^7.29.0", + "@types/jest": "^29.2.6", + "@types/node": "^18.11.18", + "@typescript-eslint/eslint-plugin": "^5.48.2", + "@typescript-eslint/parser": "^5.48.2", + "eslint": "^8.32.0", "eslint-config-prettier": "^8.3.0", - "jest": "^27.0.6", + "jest": "^29.3.1", "jest-date-mock": "^1.0.8", "npm-run-all": "^4.1.5", "prettier": "^2.3.2", - "ts-jest": "^27.0.3", + "ts-jest": "^29.0.5", "typescript": "^4.3.5" }, "scripts": { diff --git a/src/logger.ts b/src/logger.ts index 871c6d6..034cf23 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -136,7 +136,12 @@ export default class Logger { originalConsole[level] = globalConsole[level].bind(globalConsole); globalConsole[level] = async (message, ...args): Promise => { // Listen overridden console.*() function calls (type="console", level="*") - await this.onError(new Error(message), { type: 'console', level }); + await this.onError(new Error(message), { + type: 'console', + level, + args, + originalMessage: message, + }); if (!this.muting) { originalConsole[level](message, ...args); } @@ -167,9 +172,21 @@ export default class Logger { return; } - const message = this.messageFormatter - ? await this.messageFormatter(e, info) // Custom formatter - : JSON.stringify({ message: e.message, ...info }); // Simple JSON formatter + let message: string | null; + if (this.messageFormatter) { + message = await this.messageFormatter(e, info); // Custom formatter + } else { + const { args, originalMessage, ...otherInfo } = info ?? {}; + const converToString = (value: any) => + !(value instanceof Error) && typeof value === 'object' + ? JSON.stringify(value) + : value; + let msg = converToString(originalMessage ?? e.message); + if (args && Array.isArray(args)) { + msg = [msg, ...args.map(converToString)].join(' '); + } + message = JSON.stringify({ message: msg, ...otherInfo }); // Simple JSON formatter + } // Abort when received null if (!message) { diff --git a/tests/main.test.ts b/tests/main.test.ts index 9dae5d8..66b1b36 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -16,7 +16,9 @@ let globalConsole: DummyConsole; let storage: DummyStorage; let eventTarget: DummyEventTarget; -jest.useFakeTimers('legacy'); +jest.useFakeTimers({ + legacyFakeTimers: true, +}); const install = (options: Partial = {}): void => { logger = new Logger('key', 'secret', 'ap-northeast-1', 'example'); @@ -78,6 +80,20 @@ describe('Collecting errors', (): void => { ]); }); + it('should receive from console with args', async (): Promise => { + await globalConsole.error({ a: 1 }, { b: 2 }, ['a'], 'b'); + expect((logger as any).events).toStrictEqual([ + { + message: JSON.stringify({ + message: '{"a":1} {"b":2} ["a"] b', + type: 'console', + level: 'error', + }), + timestamp: 0, + }, + ]); + }); + it('should receive from custom trigger', async (): Promise => { await logger.onError(new Error('Something went wrong'), { type: 'custom' }); expect((logger as any).events).toStrictEqual([ diff --git a/tests/stub.ts b/tests/stub.ts index 2e60a40..eac6ba1 100644 --- a/tests/stub.ts +++ b/tests/stub.ts @@ -28,24 +28,45 @@ export class DummyStorage implements StorageInterface { export interface DummyConsoleMessage { message: unknown; level: Level; + args?: any[]; } export class DummyConsole implements ConsoleInterface { public messages: DummyConsoleMessage[] = []; - public debug(message?: unknown): void { - this.messages.push({ message, level: 'debug' }); + public debug(message?: unknown, ...optionalParams: any[]): void { + const msg: DummyConsoleMessage = { message, level: 'debug' }; + if (optionalParams.length) { + msg.args = optionalParams; + } + this.messages.push(msg); } - public info(message?: unknown): void { - this.messages.push({ message, level: 'info' }); + public info(message?: unknown, ...optionalParams: any[]): void { + const msg: DummyConsoleMessage = { message, level: 'info' }; + if (optionalParams.length) { + msg.args = optionalParams; + } + this.messages.push(msg); } - public log(message?: unknown): void { - this.messages.push({ message, level: 'log' }); + public log(message?: unknown, ...optionalParams: any[]): void { + const msg: DummyConsoleMessage = { message, level: 'log' }; + if (optionalParams.length) { + msg.args = optionalParams; + } + this.messages.push(msg); } - public error(message?: unknown): void { - this.messages.push({ message, level: 'error' }); + public error(message?: unknown, ...optionalParams: any[]): void { + const msg: DummyConsoleMessage = { message, level: 'error' }; + if (optionalParams.length) { + msg.args = optionalParams; + } + this.messages.push(msg); } - public warn(message?: unknown): void { - this.messages.push({ message, level: 'warn' }); + public warn(message?: unknown, ...optionalParams: any[]): void { + const msg: DummyConsoleMessage = { message, level: 'warn' }; + if (optionalParams.length) { + msg.args = optionalParams; + } + this.messages.push(msg); } } From 6fefb38269c3b757271c4d8cff93d55ee98fecc0 Mon Sep 17 00:00:00 2001 From: Vlad Hetman Date: Fri, 20 Jan 2023 09:26:49 +0200 Subject: [PATCH 2/2] func to reset cached logStreamName --- src/logger.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/logger.ts b/src/logger.ts index 034cf23..515e69d 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -101,6 +101,13 @@ export default class Logger { return this; } + /** + * Reset logStreamName for refresh from logStreamNameResolver. + */ + public async resetLogStreamName(): Promise { + await this.deleteCache('logStreamName'); + } + /** * Bootstrap Logger. *