chore: 运行日志统一中文表述

访问日志改为自管中间件(弃 hono/logger 英文格式),启动与未捕获错误文案中文化。
This commit is contained in:
2026-10-06 12:04:32 +08:00
parent 867bb7d566
commit 7ab9d32495
2 changed files with 7 additions and 4 deletions
+6 -3
View File
@@ -1,6 +1,5 @@
import { OpenAPIHono } from "@hono/zod-openapi"; import { OpenAPIHono } from "@hono/zod-openapi";
import { apiReference } from "@scalar/hono-api-reference"; import { apiReference } from "@scalar/hono-api-reference";
import { logger as honoLogger } from "hono/logger";
import { ERR, appLogger } from "@new-api-key-usage/core"; import { ERR, appLogger } from "@new-api-key-usage/core";
import { createRepos, type Db } from "@new-api-key-usage/db"; import { createRepos, type Db } from "@new-api-key-usage/db";
import { jsonError, type AppVars } from "./middleware.ts"; import { jsonError, type AppVars } from "./middleware.ts";
@@ -39,7 +38,11 @@ export function createApp(opts: CreateAppOptions) {
}); });
const repos = createRepos(opts.db); const repos = createRepos(opts.db);
app.use("*", honoLogger()); app.use("*", async (c, next) => {
const start = Date.now();
await next();
appLogger("http").info`${c.req.method} ${c.req.path} → ${c.res.status} · ${Date.now() - start}ms`;
});
app.use("*", async (c, next) => { app.use("*", async (c, next) => {
c.set("repos", repos); c.set("repos", repos);
c.set("skKeyEncryptionSecret", opts.skKeyEncryptionSecret); c.set("skKeyEncryptionSecret", opts.skKeyEncryptionSecret);
@@ -61,7 +64,7 @@ export function createApp(opts: CreateAppOptions) {
}); });
app.onError((err, c) => { app.onError((err, c) => {
appLogger("server").error`unhandled ${c.req.method} ${c.req.path}: ${err}`; appLogger("server").error`未捕获错误 ${c.req.method} ${c.req.path}:${err}`;
return jsonError(c, 500, ERR.internal()); return jsonError(c, 500, ERR.internal());
}); });
+1 -1
View File
@@ -17,7 +17,7 @@ const app = createApp({
adminInitPassword: env.ADMIN_INIT_PASSWORD, adminInitPassword: env.ADMIN_INIT_PASSWORD,
}); });
appLogger("server").info`listening on port ${env.PORT} (cookie_secure=${env.COOKIE_SECURE}, log_level=${env.LOG_LEVEL})`; appLogger("server").info`服务已启动,端口 ${env.PORT}(cookie_secure=${env.COOKIE_SECURE},日志级别=${env.LOG_LEVEL}`;
export default { export default {
port: env.PORT, port: env.PORT,