2024-09-04 23:08:11 +02:00
|
|
|
import { getLogger } from "@logtape/logtape";
|
|
|
|
|
import chalk from "chalk";
|
2024-12-18 20:42:40 +01:00
|
|
|
import { createMiddleware } from "hono/factory";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { config } from "~/packages/config-manager";
|
2024-05-06 10:19:42 +02:00
|
|
|
|
|
|
|
|
export const logger = createMiddleware(async (context, next) => {
|
|
|
|
|
if (config.logging.log_requests) {
|
2024-09-23 13:20:30 +02:00
|
|
|
const serverLogger = getLogger("server");
|
2024-09-14 17:30:02 +02:00
|
|
|
const body = await context.req.raw.clone().text();
|
2024-09-04 23:08:11 +02:00
|
|
|
|
|
|
|
|
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) {
|
2024-09-23 13:20:30 +02:00
|
|
|
serverLogger.debug`${urlAndMethod}\n${hash}\n${headers}\n${bodyLog}`;
|
2024-09-04 23:08:11 +02:00
|
|
|
} else {
|
2024-09-23 13:20:30 +02:00
|
|
|
serverLogger.debug`${urlAndMethod}`;
|
2024-09-04 23:08:11 +02:00
|
|
|
}
|
2024-05-06 10:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await next();
|
|
|
|
|
});
|