middleware.go 988 字节
package constant

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/logs"
	"time"
)

func CreateRequstLogFilter(logger *logs.BeeLogger) func(ctx *context.Context) {
	return func(ctx *context.Context) {
		requestId := fmt.Sprintf("%v.%v.%v", ctx.Input.Method(), ctx.Input.URI(), time.Now().UnixNano())
		ctx.Request.Header.Add("requestId", requestId)
		var body string = "{}"
		if ctx.Input.GetData("requestBody") != nil {
			body = string(ctx.Input.GetData("requestBody").([]byte))
		}
		logger.Debug(fmt.Sprintf("====>Recv RequestId:%s BodyData:%s", requestId, body))
	}
}

func CreateResponseLogFilter(logger *logs.BeeLogger) func(ctx *context.Context) {
	return func(ctx *context.Context) {
		requestId := ctx.Request.Header.Get("requestId")
		body, _ := json.Marshal(ctx.Input.GetData("outputData"))
		if len(body) > 1000 {
			body = body[:1000]
		}
		logger.Debug(fmt.Sprintf("<====Send RequestId:%v BodyData:%s", requestId, body))
	}
}