| @@ -20,15 +20,10 @@ module.exports = class extends think.Controller { | |||||
| async __before() { | async __before() { | ||||
| const path = this.ctx.path; | const path = this.ctx.path; | ||||
| // 记录请求开始时间 | |||||
| // 记录请求开始时间和参数 | |||||
| this._requestStart = Date.now(); | 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后缀的路径) | // 白名单放行(同时匹配带.html后缀的路径) | ||||
| const pathNoSuffix = path.replace(/\.html$/, ''); | const pathNoSuffix = path.replace(/\.html$/, ''); | ||||
| if (WHITE_LIST.includes(path) || WHITE_LIST.includes(pathNoSuffix)) { | if (WHITE_LIST.includes(path) || WHITE_LIST.includes(pathNoSuffix)) { | ||||
| @@ -76,10 +71,17 @@ module.exports = class extends think.Controller { | |||||
| async __after() { | async __after() { | ||||
| const dayjs = require('dayjs'); | const dayjs = require('dayjs'); | ||||
| const elapsed = ((Date.now() - (this._requestStart || Date.now())) / 1000).toFixed(3); | 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); | |||||
| } | } | ||||
| // 加载用户权限 | // 加载用户权限 | ||||