内网服务访问外部第三方 API 的统一网关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

30 lines
625 B

package utils
import (
"context"
"jlog"
"github.com/gin-gonic/gin"
)
type ctxKey string
const _trace_key ctxKey = "trace_id"
func TracingHandler(c *gin.Context) {
traceId := jlog.NewTraceId()
ctx := context.WithValue(c.Request.Context(), _trace_key, traceId)
c.Request = c.Request.WithContext(ctx)
c.Next()
}
func TraceLogger(ctx context.Context) *jlog.Logger {
if traceID, ok := ctx.Value(_trace_key).(string); ok {
return jlog.NewLoggerWithTraceId(traceID)
}
return jlog.NewLoggerWithTraceId("mqttResp111222333")
}
func GinLogger(c *gin.Context) *jlog.Logger {
return TraceLogger(c.Request.Context())
}