From 8337c81dec68c0bb11262f77e47a083870b2a9c1 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Wed, 24 Jul 2024 18:29:22 +0500 Subject: [PATCH 1/3] feat: add extraData parameter to info function --- lib/Logger.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/Logger.ts b/lib/Logger.ts index 61756cf3..635d4af1 100644 --- a/lib/Logger.ts +++ b/lib/Logger.ts @@ -1,7 +1,7 @@ type Parameters = string | Record | Array> | Error; type ServerLoggingCallbackOptions = {api_setCookie: boolean; logPacket: string}; type ServerLoggingCallback = (logger: Logger, options: ServerLoggingCallbackOptions) => Promise<{requestID: string}> | undefined; -type ClientLoggingCallBack = (message: string) => void; +type ClientLoggingCallBack = (message: string, extraData: Parameters) => void; type LogLine = {message: string; parameters: Parameters; onlyFlushWithOthers?: boolean; timestamp: Date}; type LoggerOptions = {serverLoggingCallback: ServerLoggingCallback; isDebug: boolean; clientLoggingCallback: ClientLoggingCallBack}; @@ -66,7 +66,7 @@ export default class Logger { * @param forceFlushToServer Should we force flushing all logs to server? * @param onlyFlushWithOthers A request will never be sent to the server if all loglines have this set to true */ - add(message: string, parameters: Parameters, forceFlushToServer: boolean, onlyFlushWithOthers = false) { + add(message: string, parameters: Parameters, forceFlushToServer: boolean, onlyFlushWithOthers = false, extraData: Parameters = '') { const length = this.logLines.push({ message, parameters, @@ -75,7 +75,7 @@ export default class Logger { }); if (this.isDebug) { - this.client(`${message} - ${JSON.stringify(parameters)}`); + this.client(`${message} - ${JSON.stringify(parameters)}`, extraData); } // If we're over the limit, flush the logs @@ -93,9 +93,9 @@ export default class Logger { * @param parameters The parameters to send along with the message * @param onlyFlushWithOthers A request will never be sent to the server if all loglines have this set to true */ - info(message: string, sendNow = false, parameters: Parameters = '', onlyFlushWithOthers = false) { + info(message: string, sendNow = false, parameters: Parameters = '', onlyFlushWithOthers = false, extraData: Parameters = '') { const msg = `[info] ${message}`; - this.add(msg, parameters, sendNow, onlyFlushWithOthers); + this.add(msg, parameters, sendNow, onlyFlushWithOthers, extraData); } /** @@ -143,11 +143,11 @@ export default class Logger { * * @param message The message to log. */ - client(message: string) { + client(message: string, extraData: Parameters = '') { if (!this.clientLoggingCallback) { return; } - this.clientLoggingCallback(message); + this.clientLoggingCallback(message, extraData); } } From 54aa0253586874ecf238534113de2944b33dc5f9 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Thu, 25 Jul 2024 17:09:09 +0500 Subject: [PATCH 2/3] test: adjust unit test for extraData argument --- __tests__/Logger-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/Logger-test.js b/__tests__/Logger-test.js index 429a4914..7c74b296 100644 --- a/__tests__/Logger-test.js +++ b/__tests__/Logger-test.js @@ -85,5 +85,5 @@ test('Test Log.hmmm()', () => { test('Test Log.client()', () => { Log.client('Test'); expect(mockClientLoggingCallback).toHaveBeenCalled(); - expect(mockClientLoggingCallback).toHaveBeenCalledWith('Test'); + expect(mockClientLoggingCallback).toHaveBeenCalledWith('Test', ''); }); From 5a47f863e4a5056366c07da6a91170978377a9fc Mon Sep 17 00:00:00 2001 From: hurali97 Date: Thu, 25 Jul 2024 17:20:08 +0500 Subject: [PATCH 3/3] test: add test for extraData when isDebug is true --- __tests__/Logger-test.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/__tests__/Logger-test.js b/__tests__/Logger-test.js index 7c74b296..fb3f7f39 100644 --- a/__tests__/Logger-test.js +++ b/__tests__/Logger-test.js @@ -8,6 +8,12 @@ const Log = new Logger({ clientLoggingCallback: mockClientLoggingCallback, }); +const DebugLog = new Logger({ + serverLoggingCallback: mockServerLoggingCallback, + clientLoggingCallback: mockClientLoggingCallback, + isDebug: true, +}); + test('Test Log.info()', () => { Log.info('Test1', false); expect(mockServerLoggingCallback).toHaveBeenCalledTimes(0); @@ -24,9 +30,14 @@ test('Test Log.info()', () => { delete packet[0].timestamp; delete packet[1].timestamp; expect(packet).toEqual([ - {message: "[info] Test1", parameters: "", }, - {message: "[info] Test2", parameters: "", }, + {message: "[info] Test1", parameters: "",}, + {message: "[info] Test2", parameters: "",}, ]); + + // Test the case where `isDebug` is `true` in `Log` instance and we pass `extraData` parameter + DebugLog.info('Test2', false, {test: 'test'}, false, {test: 'test'}); + expect(mockServerLoggingCallback).toHaveBeenCalled(); + expect(mockClientLoggingCallback).toHaveBeenCalledWith('[info] Test2 - {"test":"test"}', {test: 'test'}); }); test('Test Log.alert()', () => {