From a3e562a5ac1e7d95cbc66fee4603663b6af13648 Mon Sep 17 00:00:00 2001 From: macbookpro Date: Mon, 19 Feb 2024 14:47:47 +0200 Subject: [PATCH 1/7] API-1 - Logging API codeless block improvement --- src/logging/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/logging/index.js b/src/logging/index.js index 5e9f63a7..b9ddc6a1 100644 --- a/src/logging/index.js +++ b/src/logging/index.js @@ -98,7 +98,7 @@ export default class Logging { push(logger, logLevel, message, exception) { if (typeof message !== 'string') { - throw new Error('"message" must be a string') + message = this.convertMessageToString(message) } this.messages.push({ logger, message, exception, 'log-level': logLevel, timestamp: Date.now() }) @@ -161,4 +161,12 @@ export default class Logging { this.checkMessagesLimit() } + convertMessageToString(message) { + if(typeof message !== 'string') { + message = JSON.stringify(message) + } + + return message + } + } From 150e7d783bc4807715edb8d78383315433b8d289 Mon Sep 17 00:00:00 2001 From: macbookpro Date: Mon, 19 Feb 2024 14:51:11 +0200 Subject: [PATCH 2/7] API-1 - Logging API codeless block improvement --- src/logging/index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/logging/index.js b/src/logging/index.js index b9ddc6a1..70290648 100644 --- a/src/logging/index.js +++ b/src/logging/index.js @@ -97,11 +97,13 @@ export default class Logging { } push(logger, logLevel, message, exception) { - if (typeof message !== 'string') { - message = this.convertMessageToString(message) - } - - this.messages.push({ logger, message, exception, 'log-level': logLevel, timestamp: Date.now() }) + this.messages.push({ + logger, + message: this.convertMessageToString(message), + exception, + 'log-level': logLevel, + timestamp: Date.now() + }) this.checkMessagesLen() } @@ -168,5 +170,4 @@ export default class Logging { return message } - } From 54a0e59ee27833092a454c78b4d0cf41ecaaf27f Mon Sep 17 00:00:00 2001 From: macbookpro Date: Mon, 19 Feb 2024 15:44:17 +0200 Subject: [PATCH 3/7] API-1 - Logging API codeless block improvement(added and fixed tests) --- test/unit/specs/logging.js | 50 +++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/test/unit/specs/logging.js b/test/unit/specs/logging.js index 5f5bba42..f2ca9f4c 100644 --- a/test/unit/specs/logging.js +++ b/test/unit/specs/logging.js @@ -212,19 +212,17 @@ describe('', function() { expect(req1.body[4].exception).to.include('fatal exception') }) - it('throws an error when message is not string', async () => { - const errorMsg = '"message" must be a string' - + it('doesnt throw an error if the message is not a string', async () => { async function check(method) { - expect(() => logger[method](0)).to.throw(errorMsg) - expect(() => logger[method](123)).to.throw(errorMsg) - expect(() => logger[method](true)).to.throw(errorMsg) - expect(() => logger[method](false)).to.throw(errorMsg) - expect(() => logger[method](null)).to.throw(errorMsg) - expect(() => logger[method](undefined)).to.throw(errorMsg) - expect(() => logger[method](_ => _)).to.throw(errorMsg) - expect(() => logger[method]({ bar: 123 })).to.throw(errorMsg) - expect(() => logger[method](['foo', 123, true, false, null, undefined, { bar: 123 }])).to.throw(errorMsg) + expect(() => logger[method](0)).to.not.throw() + expect(() => logger[method](123)).to.not.throw() + expect(() => logger[method](true)).to.not.throw() + expect(() => logger[method](false)).to.not.throw() + expect(() => logger[method](null)).to.not.throw() + expect(() => logger[method](undefined)).to.not.throw() + expect(() => logger[method](_ => _)).to.not.throw() + expect(() => logger[method]({ bar: 123 })).to.not.throw() + expect(() => logger[method](['foo', 123, true, false, null, undefined, { bar: 123 }])).to.not.throw() } await check('debug') @@ -235,6 +233,34 @@ describe('', function() { await check('trace') }) + it('converts non-string values to strings', async () => { + const req = prepareMockRequest({}) + + logger.debug(0) + logger.debug(123) + logger.debug(true) + logger.debug(false) + logger.debug(null) + logger.debug(undefined) + logger.debug(_ => _) + logger.debug({foo: 'bar'}) + logger.debug(['foo', 123, true, false, null, undefined, { bar: 123 }]) + + await Utils.wait(1100) + + expect(req.body.map(b => b.message)).to.deep.equal([ + '0', + '123', + "true", + "false", + "null", + undefined, + undefined, + "{\"foo\":\"bar\"}", + "[\"foo\",123,true,false,null,null,{\"bar\":123}]" + ]) + }) + it('send messages pool by timer', async () => { const req1 = prepareMockRequest() From 36e71cfbe02e32fbd7a4a8368eb46fd579cb13ca Mon Sep 17 00:00:00 2001 From: macbookpro Date: Mon, 19 Feb 2024 15:54:21 +0200 Subject: [PATCH 4/7] API-1 - Logging API codeless block improvement(added and fixed tests) --- test/unit/specs/logging.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/specs/logging.js b/test/unit/specs/logging.js index f2ca9f4c..9772652a 100644 --- a/test/unit/specs/logging.js +++ b/test/unit/specs/logging.js @@ -246,7 +246,7 @@ describe('', function() { logger.debug({foo: 'bar'}) logger.debug(['foo', 123, true, false, null, undefined, { bar: 123 }]) - await Utils.wait(1100) + await Backendless.Logging.flush() expect(req.body.map(b => b.message)).to.deep.equal([ '0', From 4f38bda733d74180b69cefea3f19b733368718b1 Mon Sep 17 00:00:00 2001 From: macbookpro Date: Tue, 20 Feb 2024 11:55:25 +0200 Subject: [PATCH 5/7] API-1 - Logging API codeless block improvement --- src/logging/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/logging/index.js b/src/logging/index.js index 70290648..c4a51f58 100644 --- a/src/logging/index.js +++ b/src/logging/index.js @@ -99,10 +99,10 @@ export default class Logging { push(logger, logLevel, message, exception) { this.messages.push({ logger, - message: this.convertMessageToString(message), + message : convertMessageToString(message), exception, 'log-level': logLevel, - timestamp: Date.now() + timestamp : Date.now() }) this.checkMessagesLen() @@ -162,12 +162,12 @@ export default class Logging { this.checkMessagesLimit() } +} - convertMessageToString(message) { - if(typeof message !== 'string') { - message = JSON.stringify(message) - } - - return message +function convertMessageToString(message) { + if (typeof message !== 'string') { + return JSON.stringify(message) } + + return message } From ef82afdf25c34e2e057c4cfa560d01bd82f6b3fa Mon Sep 17 00:00:00 2001 From: macbookpro Date: Mon, 26 Feb 2024 12:47:02 +0200 Subject: [PATCH 6/7] API-1 - Logging API codeless block improvement(fixed convert function and tests) --- src/logging/index.js | 8 ++++++++ test/unit/specs/logging.js | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/logging/index.js b/src/logging/index.js index c4a51f58..c8d1b971 100644 --- a/src/logging/index.js +++ b/src/logging/index.js @@ -165,6 +165,14 @@ export default class Logging { } function convertMessageToString(message) { + if (typeof message === 'undefined') { + return 'undefined' + } + + if (typeof message === 'function') { + return Object.prototype.toString.call(message) + } + if (typeof message !== 'string') { return JSON.stringify(message) } diff --git a/test/unit/specs/logging.js b/test/unit/specs/logging.js index 9772652a..a33f2cc9 100644 --- a/test/unit/specs/logging.js +++ b/test/unit/specs/logging.js @@ -237,27 +237,29 @@ describe('', function() { const req = prepareMockRequest({}) logger.debug(0) + logger.debug('4') logger.debug(123) logger.debug(true) logger.debug(false) logger.debug(null) logger.debug(undefined) logger.debug(_ => _) - logger.debug({foo: 'bar'}) + logger.debug({ foo: 'bar' }) logger.debug(['foo', 123, true, false, null, undefined, { bar: 123 }]) await Backendless.Logging.flush() expect(req.body.map(b => b.message)).to.deep.equal([ '0', + '4', '123', - "true", - "false", - "null", - undefined, - undefined, - "{\"foo\":\"bar\"}", - "[\"foo\",123,true,false,null,null,{\"bar\":123}]" + 'true', + 'false', + 'null', + 'undefined', + '[object Function]', + '{\"foo\":\"bar\"}', + '[\"foo\",123,true,false,null,null,{\"bar\":123}]' ]) }) From 3a3814a12a45e24b1bc962da28245b5b77526b15 Mon Sep 17 00:00:00 2001 From: macbookpro Date: Mon, 26 Feb 2024 13:43:39 +0200 Subject: [PATCH 7/7] API-1 - Logging API codeless block improvement(fixed convert function and tests) --- src/logging/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/logging/index.js b/src/logging/index.js index c8d1b971..1ce5b78e 100644 --- a/src/logging/index.js +++ b/src/logging/index.js @@ -165,6 +165,10 @@ export default class Logging { } function convertMessageToString(message) { + if (typeof message === 'string') { + return message + } + if (typeof message === 'undefined') { return 'undefined' } @@ -173,9 +177,5 @@ function convertMessageToString(message) { return Object.prototype.toString.call(message) } - if (typeof message !== 'string') { - return JSON.stringify(message) - } - - return message + return JSON.stringify(message) }