From b0631b8771c39eff8b03f727f909010e67c160ef Mon Sep 17 00:00:00 2001 From: leiyun Date: Sat, 21 Mar 2026 00:34:24 +0800 Subject: [PATCH] save --- src/controller/base.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/controller/base.js b/src/controller/base.js index f7d8760..9faeba3 100644 --- a/src/controller/base.js +++ b/src/controller/base.js @@ -20,15 +20,10 @@ module.exports = class extends think.Controller { async __before() { const path = this.ctx.path; - // 记录请求开始时间 + // 记录请求开始时间和参数 this._requestStart = Date.now(); - const dayjs = require('dayjs'); - const params = { ...this.get(), ...this.post() }; - console.log('=================== 请求开始 ==================='); - console.log(`path: ${path}`); - console.log(`params: ${JSON.stringify(params)}`); - console.log(`time: ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`); - + this._requestPath = path; + this._requestParams = { ...this.get(), ...this.post() }; // 白名单放行(同时匹配带.html后缀的路径) const pathNoSuffix = path.replace(/\.html$/, ''); if (WHITE_LIST.includes(path) || WHITE_LIST.includes(pathNoSuffix)) { @@ -76,10 +71,17 @@ module.exports = class extends think.Controller { async __after() { const dayjs = require('dayjs'); const elapsed = ((Date.now() - (this._requestStart || Date.now())) / 1000).toFixed(3); - console.log(`result: ${JSON.stringify(this.ctx.body || '').slice(0, 500)}`); - console.log(`time: ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`); - console.log(`耗时: ${elapsed}秒`); - console.log('=================== 请求结束 ==================='); + 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); } // 加载用户权限