Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API-1 - Logging API codeless block improvement #246

Merged
19 changes: 14 additions & 5 deletions src/logging/index.js
Expand Up @@ -97,11 +97,13 @@ export default class Logging {
}

push(logger, logLevel, message, exception) {
if (typeof message !== 'string') {
throw new Error('"message" must be a string')
}

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()
}
Expand Down Expand Up @@ -161,4 +163,11 @@ export default class Logging {
this.checkMessagesLimit()
}

convertMessageToString(message) {
Valodya marked this conversation as resolved.
Show resolved Hide resolved
if(typeof message !== 'string') {
Valodya marked this conversation as resolved.
Show resolved Hide resolved
message = JSON.stringify(message)
Valodya marked this conversation as resolved.
Show resolved Hide resolved
}

return message
}
}
50 changes: 38 additions & 12 deletions test/unit/specs/logging.js
Expand Up @@ -212,19 +212,17 @@ describe('<Logging>', 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')
Expand All @@ -235,6 +233,34 @@ describe('<Logging>', 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 Backendless.Logging.flush()

expect(req.body.map(b => b.message)).to.deep.equal([
'0',
'123',
"true",
"false",
"null",
undefined,
Valodya marked this conversation as resolved.
Show resolved Hide resolved
undefined,
"{\"foo\":\"bar\"}",
"[\"foo\",123,true,false,null,null,{\"bar\":123}]"
])
})

it('send messages pool by timer', async () => {
const req1 = prepareMockRequest()

Expand Down