单独增加基于gin构建日志的方法
This commit is contained in:
32
util.go
32
util.go
@ -34,6 +34,32 @@ func getStrVal(ctx context.Context, key string) string {
|
||||
return fmt.Sprintf("%v", val)
|
||||
}
|
||||
|
||||
// NewLogDataForGin 构建gin请求的日志
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:37 2025/3/5
|
||||
func NewLogDataForGin(ctx *gin.Context, logType string, code string, logData map[string]any) *LogData {
|
||||
hostname, _ := os.Hostname()
|
||||
commonLogData := &LogData{
|
||||
Env: ginRequest.WrapperHandle.GetCtxStringData(ctx, "env", ""),
|
||||
Uri: ginRequest.WrapperHandle.GetUri(ctx, ""),
|
||||
TraceID: ginRequest.WrapperHandle.GetCtxStringData(ctx, "trace_id", ""),
|
||||
UserID: ginRequest.WrapperHandle.GetCtxStringData(ctx, "user_id", ""),
|
||||
UserRoleID: ginRequest.WrapperHandle.GetCtxStringData(ctx, "user_role_id", ""),
|
||||
OperateMode: ginRequest.WrapperHandle.GetCtxStringData(ctx, "operate_mode", ""),
|
||||
LogType: logType,
|
||||
CodeVersion: ginRequest.WrapperHandle.GetCtxStringData(ctx, "code_version", ""),
|
||||
ServiceVersion: ginRequest.WrapperHandle.GetCtxStringData(ctx, "service_version", ""),
|
||||
ClientIp: ginRequest.WrapperHandle.GetCtxStringData(ctx, "client_ip", "s"),
|
||||
ServerIp: util.IP.GetHostIP(),
|
||||
Hostname: hostname,
|
||||
Code: code,
|
||||
Data: logData,
|
||||
}
|
||||
return commonLogData
|
||||
}
|
||||
|
||||
// NewLogData ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -41,6 +67,12 @@ func getStrVal(ctx context.Context, key string) string {
|
||||
// Date : 16:21 2024/7/23
|
||||
func NewLogData(ctx context.Context, logType string, code string, logData map[string]any) *LogData {
|
||||
hostname, _ := os.Hostname()
|
||||
if nil == ctx {
|
||||
ctx = context.TODO()
|
||||
}
|
||||
if ginCtx, ok := ctx.(*gin.Context); ok {
|
||||
return NewLogDataForGin(ginCtx, logType, code, logData)
|
||||
}
|
||||
commonLogData := &LogData{
|
||||
Env: getStrVal(ctx, "env"),
|
||||
Uri: getStrVal(ctx, "uri"),
|
||||
|
Reference in New Issue
Block a user