Add logging

This commit is contained in:
Isaac 2022-05-06 00:12:38 +01:00
parent 11ec090ab6
commit e65405c183

View File

@ -1,8 +1,29 @@
const fastify = require('fastify')(); const fastify = require('fastify')();
const { readFiles } = require('node-dir'); const { short } = require('leeks.js');
const { join } = require('path'); const { join } = require('path');
const { readFiles } = require('node-dir');
module.exports = client => { 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'); const dir = join(__dirname, '/routes');
readFiles(dir, readFiles(dir,