From 9dc143060fe66bb3b4de9d7e3fb287381696b5fc Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 4 Sep 2024 23:08:11 +0200 Subject: [PATCH] refactor: :loud_sound: Don't use debugRequest for logging middleware (doesn't output a body) --- middlewares/logger.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/middlewares/logger.ts b/middlewares/logger.ts index 188f3d09..3d1ff955 100644 --- a/middlewares/logger.ts +++ b/middlewares/logger.ts @@ -1,10 +1,35 @@ -import { debugRequest } from "@/api"; import { createMiddleware } from "@hono/hono/factory"; +import { getLogger } from "@logtape/logtape"; +import chalk from "chalk"; import { config } from "~/packages/config-manager"; export const logger = createMiddleware(async (context, next) => { if (config.logging.log_requests) { - await debugRequest(context.req.raw.clone()); + const logger = getLogger("server"); + const body = await context.req.text(); + + const urlAndMethod = `${chalk.green(context.req.method)} ${chalk.blue(context.req.url)}`; + + const hash = `${chalk.bold("Hash")}: ${chalk.yellow( + new Bun.SHA256().update(body).digest("hex"), + )}`; + + const headers = `${chalk.bold("Headers")}:\n${Array.from( + context.req.raw.headers.entries(), + ) + .map( + ([key, value]) => + ` - ${chalk.cyan(key)}: ${chalk.white(value)}`, + ) + .join("\n")}`; + + const bodyLog = `${chalk.bold("Body")}: ${chalk.gray(body)}`; + + if (config.logging.log_requests_verbose) { + logger.debug`${urlAndMethod}\n${hash}\n${headers}\n${bodyLog}`; + } else { + logger.debug`${urlAndMethod}`; + } } await next();