From c0fde6aa1431b478ffb1a44cd5491f1b9c032e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 12 Aug 2022 17:08:13 +0800 Subject: [PATCH] - update api - update gin --- api_gorm.go | 17 ++++++++++------- gin_gorm.go | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/api_gorm.go b/api_gorm.go index 514cf4b..e864658 100644 --- a/api_gorm.go +++ b/api_gorm.go @@ -1,8 +1,10 @@ package golog import ( + "context" "go.dtapp.net/dorm" "go.dtapp.net/gojson" + "go.dtapp.net/golog/gin_middleware" "go.dtapp.net/gorequest" "gorm.io/datatypes" "gorm.io/gorm" @@ -33,7 +35,7 @@ type apiPostgresqlLog struct { } // 记录日志 -func (c *ApiClient) gormRecord(postgresqlLog apiPostgresqlLog) error { +func (c *ApiClient) gormRecord(ctx context.Context, postgresqlLog apiPostgresqlLog) error { if utf8.ValidString(string(postgresqlLog.ResponseBody)) == false { postgresqlLog.ResponseBody = datatypes.JSON("") @@ -44,6 +46,7 @@ func (c *ApiClient) gormRecord(postgresqlLog apiPostgresqlLog) error { postgresqlLog.SystemInsideIp = c.config.insideIp } postgresqlLog.GoVersion = c.config.goVersion + postgresqlLog.TraceId = gin_middleware.GetTraceIdContext(ctx) return c.gormClient.Table(c.config.tableName).Create(&postgresqlLog).Error } @@ -54,11 +57,11 @@ func (c *ApiClient) GormQuery() *gorm.DB { } // GormMiddleware 中间件 -func (c *ApiClient) GormMiddleware(request gorequest.Response, sdkVersion string) { +func (c *ApiClient) GormMiddleware(ctx context.Context, request gorequest.Response, sdkVersion string) { if request.ResponseHeader.Get("Content-Type") == "image/jpeg" || request.ResponseHeader.Get("Content-Type") == "image/png" { return } - c.gormRecord(apiPostgresqlLog{ + c.gormRecord(ctx, apiPostgresqlLog{ RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 @@ -76,8 +79,8 @@ func (c *ApiClient) GormMiddleware(request gorequest.Response, sdkVersion string } // GormMiddlewareXml 中间件 -func (c *ApiClient) GormMiddlewareXml(request gorequest.Response, sdkVersion string) { - c.gormRecord(apiPostgresqlLog{ +func (c *ApiClient) GormMiddlewareXml(ctx context.Context, request gorequest.Response, sdkVersion string) { + c.gormRecord(ctx, apiPostgresqlLog{ RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 @@ -95,8 +98,8 @@ func (c *ApiClient) GormMiddlewareXml(request gorequest.Response, sdkVersion str } // GormMiddlewareCustom 中间件 -func (c *ApiClient) GormMiddlewareCustom(api string, request gorequest.Response, sdkVersion string) { - c.gormRecord(apiPostgresqlLog{ +func (c *ApiClient) GormMiddlewareCustom(ctx context.Context, api string, request gorequest.Response, sdkVersion string) { + c.gormRecord(ctx, apiPostgresqlLog{ RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 diff --git a/gin_gorm.go b/gin_gorm.go index 19e667e..6fd2c38 100644 --- a/gin_gorm.go +++ b/gin_gorm.go @@ -5,6 +5,7 @@ import ( "encoding/json" "github.com/gin-gonic/gin" "go.dtapp.net/gojson" + "go.dtapp.net/golog/gin_middleware" "go.dtapp.net/gorequest" "go.dtapp.net/gotime" "go.dtapp.net/goxml" @@ -131,7 +132,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc { } if len(jsonBody) > 0 { c.gormRecord(ginPostgresqlLog{ - TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号 + TraceId: gin_middleware.GetTraceId(ginCtx), //【系统】链编号 RequestTime: requestTime, //【请求】时间 RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接 RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 @@ -156,7 +157,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc { }) } else { c.gormRecord(ginPostgresqlLog{ - TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号 + TraceId: gin_middleware.GetTraceId(ginCtx), //【系统】链编号 RequestTime: requestTime, //【请求】时间 RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接 RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接