Last active
December 1, 2017 13:44
-
-
Save bitkidd/2fa33b30b18193aae670ca7e3b764e40 to your computer and use it in GitHub Desktop.
adonis logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const colors = use('colors/safe') | |
const Env = use('Env') | |
const colorStatus = function (status) { | |
let colorFn = status >= 500 ? colors.red // red | |
: status >= 400 ? colors.yellow // yellow | |
: status >= 300 ? colors.cyan // cyan | |
: status >= 200 ? colors.green // green | |
: null // no color | |
return colorFn ? colorFn(status) : status | |
} | |
class Logger { | |
async handle ({ request, response }, next) { | |
// Get high resolution real time at the start of the request | |
let t = process.hrtime() | |
// Pass to next middleware | |
await next() | |
// Get time difference to request start time | |
t = process.hrtime(t) | |
// Color status, output method, url, status and time | |
const status = colorStatus(response.response.statusCode) | |
const ms = colors.yellow(`${ ( (t[ 0 ] * 1000 + t[ 1 ] / 1000) / 1000).toFixed(2) } ms`) | |
const params = colors.gray( JSON.stringify( request.all() ) ) | |
console.log(`[${request.method()} - ${status}] [${ms}] ${request.url()} | ${params}`) | |
} | |
} | |
module.exports = Logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment