mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
feat(api): ✨ Add response logging
This commit is contained in:
parent
b5411c01e4
commit
5d2aa82247
|
|
@ -3848,6 +3848,10 @@
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false
|
"default": false
|
||||||
},
|
},
|
||||||
|
"log_responses": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
"log_requests_verbose": {
|
"log_requests_verbose": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false
|
"default": false
|
||||||
|
|
@ -3941,6 +3945,7 @@
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
"default": {
|
"default": {
|
||||||
"log_requests": false,
|
"log_requests": false,
|
||||||
|
"log_responses": false,
|
||||||
"log_requests_verbose": false,
|
"log_requests_verbose": false,
|
||||||
"log_level": "info",
|
"log_level": "info",
|
||||||
"log_ip": false,
|
"log_ip": false,
|
||||||
|
|
|
||||||
|
|
@ -567,6 +567,7 @@ export const configValidator = z.object({
|
||||||
logging: z
|
logging: z
|
||||||
.object({
|
.object({
|
||||||
log_requests: z.boolean().default(false),
|
log_requests: z.boolean().default(false),
|
||||||
|
log_responses: z.boolean().default(false),
|
||||||
log_requests_verbose: z.boolean().default(false),
|
log_requests_verbose: z.boolean().default(false),
|
||||||
log_level: z
|
log_level: z
|
||||||
.enum(["debug", "info", "warning", "error", "fatal"])
|
.enum(["debug", "info", "warning", "error", "fatal"])
|
||||||
|
|
@ -605,6 +606,7 @@ export const configValidator = z.object({
|
||||||
})
|
})
|
||||||
.default({
|
.default({
|
||||||
log_requests: false,
|
log_requests: false,
|
||||||
|
log_responses: false,
|
||||||
log_requests_verbose: false,
|
log_requests_verbose: false,
|
||||||
log_level: "info",
|
log_level: "info",
|
||||||
log_ip: false,
|
log_ip: false,
|
||||||
|
|
|
||||||
21
utils/api.ts
21
utils/api.ts
|
|
@ -458,3 +458,24 @@ export const debugRequest = async (req: Request) => {
|
||||||
logger.debug`${urlAndMethod}`;
|
logger.debug`${urlAndMethod}`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const debugResponse = async (res: Response) => {
|
||||||
|
const body = await res.clone().text();
|
||||||
|
const logger = getLogger("server");
|
||||||
|
|
||||||
|
const status = `${chalk.bold("Status")}: ${chalk.green(res.status)}`;
|
||||||
|
|
||||||
|
const headers = `${chalk.bold("Headers")}:\n${Array.from(
|
||||||
|
res.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`${status}\n${headers}\n${bodyLog}`;
|
||||||
|
} else {
|
||||||
|
logger.debug`${status}`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import type { OpenAPIHono } from "@hono/zod-openapi";
|
import type { OpenAPIHono } from "@hono/zod-openapi";
|
||||||
import type { Config } from "~/packages/config-manager/config.type";
|
import type { Config } from "~/packages/config-manager/config.type";
|
||||||
import type { HonoEnv } from "~/types/api";
|
import type { HonoEnv } from "~/types/api";
|
||||||
|
import { debugResponse } from "./api";
|
||||||
|
|
||||||
export const createServer = (config: Config, app: OpenAPIHono<HonoEnv>) =>
|
export const createServer = (config: Config, app: OpenAPIHono<HonoEnv>) =>
|
||||||
Bun.serve({
|
Bun.serve({
|
||||||
|
|
@ -17,7 +18,13 @@ export const createServer = (config: Config, app: OpenAPIHono<HonoEnv>) =>
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
hostname: config.http.bind || "0.0.0.0", // defaults to "0.0.0.0"
|
hostname: config.http.bind || "0.0.0.0", // defaults to "0.0.0.0"
|
||||||
fetch(req, server) {
|
async fetch(req, server) {
|
||||||
return app.fetch(req, { ip: server.requestIP(req) });
|
const output = await app.fetch(req, { ip: server.requestIP(req) });
|
||||||
|
|
||||||
|
if (config.logging.log_responses) {
|
||||||
|
await debugResponse(output.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue