From ced9316fcd1f89cf680601c4c97f91ba64fe6ee7 Mon Sep 17 00:00:00 2001 From: leiyun Date: Sat, 21 Mar 2026 00:42:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/middleware.js | 5 +++++ src/controller/base.js | 21 ------------------- src/middleware/request_log.js | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 src/middleware/request_log.js diff --git a/src/config/middleware.js b/src/config/middleware.js index 488a8f7..61282f1 100644 --- a/src/config/middleware.js +++ b/src/config/middleware.js @@ -1,7 +1,12 @@ const path = require('path'); const isDev = think.env === 'development'; +const requestLog = require('../middleware/request_log.js'); module.exports = [ + { + handle: requestLog, + options: {} + }, { handle: 'meta', options: { diff --git a/src/controller/base.js b/src/controller/base.js index 9faeba3..f22ebe1 100644 --- a/src/controller/base.js +++ b/src/controller/base.js @@ -19,11 +19,6 @@ const WHITE_LIST = [ module.exports = class extends think.Controller { async __before() { const path = this.ctx.path; - - // 记录请求开始时间和参数 - this._requestStart = Date.now(); - this._requestPath = path; - this._requestParams = { ...this.get(), ...this.post() }; // 白名单放行(同时匹配带.html后缀的路径) const pathNoSuffix = path.replace(/\.html$/, ''); if (WHITE_LIST.includes(path) || WHITE_LIST.includes(pathNoSuffix)) { @@ -68,22 +63,6 @@ module.exports = class extends think.Controller { } } - async __after() { - const dayjs = require('dayjs'); - const elapsed = ((Date.now() - (this._requestStart || Date.now())) / 1000).toFixed(3); - const result = JSON.stringify(this.ctx.body || '').slice(0, 500); - const log = [ - '=================== 请求日志 ===================', - `path: ${this._requestPath || this.ctx.path}`, - `params: ${JSON.stringify(this._requestParams || {})}`, - `result: ${result}`, - `time: ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`, - `耗时: ${elapsed}秒`, - '=================== 请求结束 ===================', - ].join('\n'); - console.log(log); - } - // 加载用户权限 async loadUserPermissions() { if (!this.adminUser) return; diff --git a/src/middleware/request_log.js b/src/middleware/request_log.js new file mode 100644 index 0000000..5bcc69d --- /dev/null +++ b/src/middleware/request_log.js @@ -0,0 +1,38 @@ +const dayjs = require('dayjs'); + +module.exports = () => { + return async (ctx, next) => { + // 跳过静态资源和source map + if (ctx.path.startsWith('/static/') || ctx.path === '/favicon.ico' || ctx.path.endsWith('.map')) { + return next(); + } + + const start = Date.now(); + const params = { ...ctx.query, ...(ctx.request.body || {}) }; + + try { + await next(); + } catch (e) { + throw e; + } finally { + // 只打印 JSON 接口响应(this.success / this.fail / this.json) + const body = ctx.body; + const isJson = body && typeof body === 'object' && ('code' in body || 'errno' in body || 'data' in body); + if (isJson) { + const elapsed = ((Date.now() - start) / 1000).toFixed(3); + const result = JSON.stringify(body).slice(0, 500); + const log = [ + '=================== 请求日志 ===================', + `path: ${ctx.path}`, + `method: ${ctx.method}`, + `params: ${JSON.stringify(params)}`, + `result: ${result}`, + `time: ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`, + `耗时: ${elapsed}秒`, + '=================== 请求结束 ===================', + ].join('\n'); + console.log(log); + } + } + }; +};