Skip to content

Commit 08fb775

Browse files
andrsvsrgmacbookpro
andauthored
API-1 - Logging API codeless block improvement (#246)
* API-1 - Logging API codeless block improvement * API-1 - Logging API codeless block improvement * API-1 - Logging API codeless block improvement(added and fixed tests) * API-1 - Logging API codeless block improvement(added and fixed tests) * API-1 - Logging API codeless block improvement * API-1 - Logging API codeless block improvement(fixed convert function and tests) * API-1 - Logging API codeless block improvement(fixed convert function and tests) --------- Co-authored-by: macbookpro <sergey.androsov@backendlessmail.com>
1 parent 49c9fc9 commit 08fb775

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

src/logging/index.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ export default class Logging {
9797
}
9898

9999
push(logger, logLevel, message, exception) {
100-
if (typeof message !== 'string') {
101-
throw new Error('"message" must be a string')
102-
}
103-
104-
this.messages.push({ logger, message, exception, 'log-level': logLevel, timestamp: Date.now() })
100+
this.messages.push({
101+
logger,
102+
message : convertMessageToString(message),
103+
exception,
104+
'log-level': logLevel,
105+
timestamp : Date.now()
106+
})
105107

106108
this.checkMessagesLen()
107109
}
@@ -160,5 +162,20 @@ export default class Logging {
160162

161163
this.checkMessagesLimit()
162164
}
165+
}
166+
167+
function convertMessageToString(message) {
168+
if (typeof message === 'string') {
169+
return message
170+
}
171+
172+
if (typeof message === 'undefined') {
173+
return 'undefined'
174+
}
175+
176+
if (typeof message === 'function') {
177+
return Object.prototype.toString.call(message)
178+
}
163179

180+
return JSON.stringify(message)
164181
}

test/unit/specs/logging.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,17 @@ describe('<Logging>', function() {
212212
expect(req1.body[4].exception).to.include('fatal exception')
213213
})
214214

215-
it('throws an error when message is not string', async () => {
216-
const errorMsg = '"message" must be a string'
217-
215+
it('doesnt throw an error if the message is not a string', async () => {
218216
async function check(method) {
219-
expect(() => logger[method](0)).to.throw(errorMsg)
220-
expect(() => logger[method](123)).to.throw(errorMsg)
221-
expect(() => logger[method](true)).to.throw(errorMsg)
222-
expect(() => logger[method](false)).to.throw(errorMsg)
223-
expect(() => logger[method](null)).to.throw(errorMsg)
224-
expect(() => logger[method](undefined)).to.throw(errorMsg)
225-
expect(() => logger[method](_ => _)).to.throw(errorMsg)
226-
expect(() => logger[method]({ bar: 123 })).to.throw(errorMsg)
227-
expect(() => logger[method](['foo', 123, true, false, null, undefined, { bar: 123 }])).to.throw(errorMsg)
217+
expect(() => logger[method](0)).to.not.throw()
218+
expect(() => logger[method](123)).to.not.throw()
219+
expect(() => logger[method](true)).to.not.throw()
220+
expect(() => logger[method](false)).to.not.throw()
221+
expect(() => logger[method](null)).to.not.throw()
222+
expect(() => logger[method](undefined)).to.not.throw()
223+
expect(() => logger[method](_ => _)).to.not.throw()
224+
expect(() => logger[method]({ bar: 123 })).to.not.throw()
225+
expect(() => logger[method](['foo', 123, true, false, null, undefined, { bar: 123 }])).to.not.throw()
228226
}
229227

230228
await check('debug')
@@ -235,6 +233,36 @@ describe('<Logging>', function() {
235233
await check('trace')
236234
})
237235

236+
it('converts non-string values to strings', async () => {
237+
const req = prepareMockRequest({})
238+
239+
logger.debug(0)
240+
logger.debug('4')
241+
logger.debug(123)
242+
logger.debug(true)
243+
logger.debug(false)
244+
logger.debug(null)
245+
logger.debug(undefined)
246+
logger.debug(_ => _)
247+
logger.debug({ foo: 'bar' })
248+
logger.debug(['foo', 123, true, false, null, undefined, { bar: 123 }])
249+
250+
await Backendless.Logging.flush()
251+
252+
expect(req.body.map(b => b.message)).to.deep.equal([
253+
'0',
254+
'4',
255+
'123',
256+
'true',
257+
'false',
258+
'null',
259+
'undefined',
260+
'[object Function]',
261+
'{\"foo\":\"bar\"}',
262+
'[\"foo\",123,true,false,null,null,{\"bar\":123}]'
263+
])
264+
})
265+
238266
it('send messages pool by timer', async () => {
239267
const req1 = prepareMockRequest()
240268

0 commit comments

Comments
 (0)