1.app 目錄新建middleware 蝙茶,創(chuàng)建httplog.js
const dayjs = request('dayjs');
const fs = require("fs");
module.exports = options =>{
return async (ctx , next)=>{
console.log("這里是配置的參數(shù)" , options)
const sTime = Date.now();
const startTime = dayjs(Date.now()).format("YYYY-MM-DD HH:mm:ss");
const req = ctx.request;
await next();
const log = {
method:req.method,
url:req.url,
data:req.body,
startTime,
endTime: dayjs(Date.now()).format("YYYY-MM-DD HH:mm:ss"),
timeLength:Date.now() - sTime
}
const data = dayjs(Date.now().format("YYYY-MM-DD HH:mm:ss"))+ "[httplog]"+JSON.stringify(log)+"\r\n";
fs.appendFileSync(ctx.app.baseDir+ 'httpLog.log',data)
}
}
2.在config.default.js中配置 (config目錄)
config.middleware = ["httplog"]
3.中間件參數(shù)配置:
在config.default.js 添加
config.middleware = ["httplog"]
config.httpLog = {
type:'all'
}
打開httpLog
module.exports = options =>{
return async (ctx , next)=>{
console.log("這里是配置的參數(shù)" , options)