mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
refactor: 🔊 Don't use debugRequest for logging middleware (doesn't output a body)
This commit is contained in:
parent
128a21cd47
commit
9dc143060f
|
|
@ -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();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue