refactor: 🔊 Don't use debugRequest for logging middleware (doesn't output a body)

This commit is contained in:
Jesse Wierzbinski 2024-09-04 23:08:11 +02:00
parent 128a21cd47
commit 9dc143060f
No known key found for this signature in database

View file

@ -1,10 +1,35 @@
import { debugRequest } from "@/api";
import { createMiddleware } from "@hono/hono/factory"; import { createMiddleware } from "@hono/hono/factory";
import { getLogger } from "@logtape/logtape";
import chalk from "chalk";
import { config } from "~/packages/config-manager"; import { config } from "~/packages/config-manager";
export const logger = createMiddleware(async (context, next) => { export const logger = createMiddleware(async (context, next) => {
if (config.logging.log_requests) { 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(); await next();