fix
This commit is contained in:
parent
20d655973a
commit
8f533f5b04
@ -22,15 +22,17 @@ var (
|
|||||||
//
|
//
|
||||||
// Date : 20:41 2022/6/25
|
// Date : 20:41 2022/6/25
|
||||||
type HttpHandleConfig struct {
|
type HttpHandleConfig struct {
|
||||||
RequestIDField string
|
RecordRequestDataField string
|
||||||
TraceIDField string
|
RecordResponseDataField string
|
||||||
ResponseCodeField string
|
RequestIDField string
|
||||||
ResponseMessageField string
|
TraceIDField string
|
||||||
HandleRequestCostField string
|
ResponseCodeField string
|
||||||
ResponseDataField string
|
ResponseMessageField string
|
||||||
ResponseTraceIDField string
|
HandleRequestCostField string
|
||||||
StartRequestTimeField string
|
ResponseDataField string
|
||||||
FinishRequestTimeField string
|
ResponseTraceIDField string
|
||||||
|
StartRequestTimeField string
|
||||||
|
FinishRequestTimeField string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConvertDefaultConfig 覆盖默认配置
|
// ConvertDefaultConfig 覆盖默认配置
|
||||||
@ -94,5 +96,15 @@ func GetHttpHandleConfig() *HttpHandleConfig {
|
|||||||
consts.GinFinishRequestTimeField,
|
consts.GinFinishRequestTimeField,
|
||||||
wrapper.String(inputHttpHandleConfig.FinishRequestTimeField),
|
wrapper.String(inputHttpHandleConfig.FinishRequestTimeField),
|
||||||
).Value(),
|
).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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,10 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/gin/define"
|
"git.zhangdeman.cn/zhangdeman/gin/define"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -41,7 +42,8 @@ func fillCfg(cfg *AccessConfig) {
|
|||||||
if nil == cfg.ExtraFieldList {
|
if nil == cfg.ExtraFieldList {
|
||||||
cfg.ExtraFieldList = make([]string, 0)
|
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
|
// getLogRequestHeader 获取记录的请求header
|
||||||
@ -54,7 +56,7 @@ func getLogRequestHeader(ctx *gin.Context, cfg *AccessConfig) map[string][]strin
|
|||||||
headerTable := make(map[string][]string)
|
headerTable := make(map[string][]string)
|
||||||
if len(cfg.RequestHeaderList) == 0 {
|
if len(cfg.RequestHeaderList) == 0 {
|
||||||
// 全部记录
|
// 全部记录
|
||||||
for headerKey, _ := range ctx.Request.Header {
|
for headerKey := range ctx.Request.Header {
|
||||||
cfg.RequestHeaderList = append(cfg.RequestHeaderList, headerKey)
|
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)
|
responseHeaderTable := make(map[string][]string)
|
||||||
if len(cfg.ResponseHeaderList) == 0 {
|
if len(cfg.ResponseHeaderList) == 0 {
|
||||||
// 全部记录
|
// 全部记录
|
||||||
for headerKey, _ := range ctx.Writer.Header() {
|
for headerKey := range ctx.Writer.Header() {
|
||||||
cfg.ResponseHeaderList = append(cfg.ResponseHeaderList, headerKey)
|
cfg.ResponseHeaderList = append(cfg.ResponseHeaderList, headerKey)
|
||||||
}
|
}
|
||||||
zap.Any("pkg_gin_response_header", ctx.Writer.Header())
|
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
|
// Date : 10:55 2022/7/14
|
||||||
func LogRequest(cfg *AccessConfig) gin.HandlerFunc {
|
func LogRequest(cfg *AccessConfig) gin.HandlerFunc {
|
||||||
fillCfg(cfg)
|
fillCfg(cfg)
|
||||||
|
handleConfig := define.GetHttpHandleConfig()
|
||||||
return func(ctx *gin.Context) {
|
return func(ctx *gin.Context) {
|
||||||
// 未传入配置或者未传入日志实例
|
// 未传入配置或者未传入日志实例
|
||||||
if nil == cfg || nil == cfg.Logger || !cfg.IsRecordLog(ctx) {
|
if nil == cfg || nil == cfg.Logger || !cfg.IsRecordLog(ctx) {
|
||||||
@ -99,16 +102,16 @@ func LogRequest(cfg *AccessConfig) gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
startRequestTime := wrapper.OwnTime(ctx.GetTime(define.StartRequestTimeField))
|
startRequestTime := wrapper.OwnTime(ctx.GetTime(handleConfig.StartRequestTimeField))
|
||||||
logDataList := []zap.Field{
|
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 = append(
|
||||||
logDataList,
|
logDataList,
|
||||||
zap.Any(define.FinishRequestTimeField, finishRequestTime.FormatUnixMilli()), // 请求完成时间
|
zap.Any(handleConfig.FinishRequestTimeField, finishRequestTime.FormatUnixMilli()), // 请求完成时间
|
||||||
zap.Int64("pkg_gin_request_cost", finishRequestTime.UnixMilli()-startRequestTime.UnixMilli()), // 接口耗时
|
zap.Int64("pkg_gin_request_cost", finishRequestTime.UnixMilli()-startRequestTime.UnixMilli()), // 接口耗时
|
||||||
zap.Any("pkg_gin_request_header", getLogRequestHeader(ctx, cfg)), // 请求header
|
zap.Any("pkg_gin_request_header", getLogRequestHeader(ctx, cfg)), // 请求header
|
||||||
zap.Any("pkg_gin_response_header", getLogResponseHeader(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, 自动触发
|
// hook 不为nil, 自动触发
|
||||||
cfg.FinishHook(
|
cfg.FinishHook(
|
||||||
ctx,
|
ctx,
|
||||||
[]byte(ctx.GetString(define.RecordRequestDataField)),
|
[]byte(ctx.GetString(handleConfig.RecordRequestDataField)),
|
||||||
ctx.GetStringMap(define.RecordResponseDataField),
|
ctx.GetStringMap(handleConfig.RecordResponseDataField),
|
||||||
finishRequestTime.UnixMilli()-startRequestTime.UnixMilli(),
|
finishRequestTime.UnixMilli()-startRequestTime.UnixMilli(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user