This commit is contained in:
白茶清欢 2024-07-23 21:34:40 +08:00
parent 20d655973a
commit 8f533f5b04
2 changed files with 34 additions and 19 deletions

View File

@ -22,15 +22,17 @@ var (
//
// Date : 20:41 2022/6/25
type HttpHandleConfig struct {
RequestIDField string
TraceIDField string
ResponseCodeField string
ResponseMessageField string
HandleRequestCostField string
ResponseDataField string
ResponseTraceIDField string
StartRequestTimeField string
FinishRequestTimeField string
RecordRequestDataField string
RecordResponseDataField string
RequestIDField string
TraceIDField string
ResponseCodeField string
ResponseMessageField string
HandleRequestCostField string
ResponseDataField string
ResponseTraceIDField string
StartRequestTimeField string
FinishRequestTimeField string
}
// ConvertDefaultConfig 覆盖默认配置
@ -94,5 +96,15 @@ func GetHttpHandleConfig() *HttpHandleConfig {
consts.GinFinishRequestTimeField,
wrapper.String(inputHttpHandleConfig.FinishRequestTimeField),
).Value(),
RecordRequestDataField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.RecordRequestDataField == "",
consts.GinRecordRequestDataField,
wrapper.String(inputHttpHandleConfig.RecordRequestDataField),
).Value(),
RecordResponseDataField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.RecordResponseDataField == "",
consts.GinRecordResponseDataField,
wrapper.String(inputHttpHandleConfig.RecordResponseDataField),
).Value(),
}
}

View File

@ -8,9 +8,10 @@
package middleware
import (
"git.zhangdeman.cn/zhangdeman/wrapper"
"strings"
"git.zhangdeman.cn/zhangdeman/wrapper"
"git.zhangdeman.cn/zhangdeman/gin/define"
"github.com/gin-gonic/gin"
@ -41,7 +42,8 @@ func fillCfg(cfg *AccessConfig) {
if nil == cfg.ExtraFieldList {
cfg.ExtraFieldList = make([]string, 0)
}
cfg.ExtraFieldList = append(cfg.ExtraFieldList, define.RecordRequestDataField, define.RecordResponseDataField)
handleConfig := define.GetHttpHandleConfig()
cfg.ExtraFieldList = append(cfg.ExtraFieldList, handleConfig.RecordRequestDataField, handleConfig.RecordResponseDataField)
}
// getLogRequestHeader 获取记录的请求header
@ -54,7 +56,7 @@ func getLogRequestHeader(ctx *gin.Context, cfg *AccessConfig) map[string][]strin
headerTable := make(map[string][]string)
if len(cfg.RequestHeaderList) == 0 {
// 全部记录
for headerKey, _ := range ctx.Request.Header {
for headerKey := range ctx.Request.Header {
cfg.RequestHeaderList = append(cfg.RequestHeaderList, headerKey)
}
}
@ -74,7 +76,7 @@ func getLogResponseHeader(ctx *gin.Context, cfg *AccessConfig) map[string][]stri
responseHeaderTable := make(map[string][]string)
if len(cfg.ResponseHeaderList) == 0 {
// 全部记录
for headerKey, _ := range ctx.Writer.Header() {
for headerKey := range ctx.Writer.Header() {
cfg.ResponseHeaderList = append(cfg.ResponseHeaderList, headerKey)
}
zap.Any("pkg_gin_response_header", ctx.Writer.Header())
@ -92,6 +94,7 @@ func getLogResponseHeader(ctx *gin.Context, cfg *AccessConfig) map[string][]stri
// Date : 10:55 2022/7/14
func LogRequest(cfg *AccessConfig) gin.HandlerFunc {
fillCfg(cfg)
handleConfig := define.GetHttpHandleConfig()
return func(ctx *gin.Context) {
// 未传入配置或者未传入日志实例
if nil == cfg || nil == cfg.Logger || !cfg.IsRecordLog(ctx) {
@ -99,16 +102,16 @@ func LogRequest(cfg *AccessConfig) gin.HandlerFunc {
return
}
ctx.Next()
startRequestTime := wrapper.OwnTime(ctx.GetTime(define.StartRequestTimeField))
startRequestTime := wrapper.OwnTime(ctx.GetTime(handleConfig.StartRequestTimeField))
logDataList := []zap.Field{
// 开始请求时间
zap.Any(define.StartRequestTimeField, startRequestTime.FormatUnixMilli()),
zap.Any(handleConfig.StartRequestTimeField, startRequestTime.FormatUnixMilli()),
}
// 结束时间
finishRequestTime := wrapper.OwnTime(ctx.GetTime(define.FinishRequestTimeField))
finishRequestTime := wrapper.OwnTime(ctx.GetTime(handleConfig.FinishRequestTimeField))
logDataList = append(
logDataList,
zap.Any(define.FinishRequestTimeField, finishRequestTime.FormatUnixMilli()), // 请求完成时间
zap.Any(handleConfig.FinishRequestTimeField, finishRequestTime.FormatUnixMilli()), // 请求完成时间
zap.Int64("pkg_gin_request_cost", finishRequestTime.UnixMilli()-startRequestTime.UnixMilli()), // 接口耗时
zap.Any("pkg_gin_request_header", getLogRequestHeader(ctx, cfg)), // 请求header
zap.Any("pkg_gin_response_header", getLogResponseHeader(ctx, cfg)), // 响应header
@ -126,8 +129,8 @@ func LogRequest(cfg *AccessConfig) gin.HandlerFunc {
// hook 不为nil, 自动触发
cfg.FinishHook(
ctx,
[]byte(ctx.GetString(define.RecordRequestDataField)),
ctx.GetStringMap(define.RecordResponseDataField),
[]byte(ctx.GetString(handleConfig.RecordRequestDataField)),
ctx.GetStringMap(handleConfig.RecordResponseDataField),
finishRequestTime.UnixMilli()-startRequestTime.UnixMilli(),
)