From e65405c183e767d3919889d064790aa565a033f9 Mon Sep 17 00:00:00 2001 From: Isaac Date: Fri, 6 May 2022 00:12:38 +0100 Subject: [PATCH] Add logging --- src/http.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/http.js b/src/http.js index 387d5f3..4102191 100644 --- a/src/http.js +++ b/src/http.js @@ -1,8 +1,29 @@ const fastify = require('fastify')(); -const { readFiles } = require('node-dir'); +const { short } = require('leeks.js'); const { join } = require('path'); +const { readFiles } = require('node-dir'); module.exports = client => { + fastify.addHook('onResponse', (req, res, done) => { + done(); + const status = (res.statusCode >= 500 + ? '&4' + : res.statusCode >= 400 + ? '&6' + : res.statusCode >= 300 + ? '&3' + : res.statusCode >= 200 + ? '&2' + : '&f') + res.statusCode; + let response_time = res.getResponseTime().toFixed(2); + response_time = (response_time >= 20 + ? '&c' + : response_time >= 5 + ? '&e' + : '&a') + response_time + 'ms'; + client.log.info.http(short(`${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${response_time}`)); + }); + const dir = join(__dirname, '/routes'); readFiles(dir,