Skip to content
This repository has been archived by the owner on Oct 19, 2018. It is now read-only.

Commit

Permalink
fix: Logger no longer doesn't log when the response is an object.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaptard committed Sep 30, 2018
1 parent c6d7442 commit 850d5a0
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions middleware/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,27 @@ class Logger {
_json(body)
} else {
_send(body)
}

// Log request
log += `${_this.prefix}< ${_this.user.uid}: ${req.method} ${req.url}\n`

// Log response
let io = '> '
if (res.statusCode.toString()[0] < 4) {
io = chalk.green(io)
} else {
io = chalk.red(io)
}
if (body) {
log += `${prefix}${io}${res.statusCode}: ${typeof body === 'string' ? body.replace(/\r?\n|\r/g, ' ').slice(0, 100) : body}${body.length > 100 ? '...' : ''}\n`
}
// Log request
log += `${_this.prefix}< ${_this.user.uid}: ${req.method} ${req.url}\n`

// Log time
let diff = process.hrtime(timestart)
log += `${prefix}${chalk.grey(`> ${(diff[0] * 1e9 + diff[1]) / 1e6} ms`)}\n`
cubic.log.info(log)
// Log response
let io = '> '
if (res.statusCode.toString()[0] < 4) {
io = chalk.green(io)
} else {
io = chalk.red(io)
}
if (body) {
const out = typeof body === 'object' ? JSON.stringify(body) : body
log += `${prefix}${io}${res.statusCode}: ${out.replace(/\r?\n|\r/g, ' ').slice(0, 100)}${out.length > 100 ? '...' : ''}\n`
}

// Log time
let diff = process.hrtime(timestart)
log += `${prefix}${chalk.grey(`> ${(diff[0] * 1e9 + diff[1]) / 1e6} ms`)}\n`
cubic.log.info(log)

// Disable json/send so we won't get "cant set after sent" errors
res.send = res.json = () => {}
Expand Down

0 comments on commit 850d5a0

Please sign in to comment.