优化日志数据生成

This commit is contained in:
2024-07-24 22:43:09 +08:00
parent 76e459ffcc
commit 80b1484ef0
3 changed files with 29 additions and 45 deletions

41
util.go
View File

@ -8,39 +8,54 @@
package logger
import (
"context"
"fmt"
"os"
"github.com/gin-gonic/gin"
ginRequest "git.zhangdeman.cn/zhangdeman/gin/request"
"git.zhangdeman.cn/zhangdeman/network/util"
"git.zhangdeman.cn/zhangdeman/serialize"
"go.uber.org/zap"
)
func getStrVal(ctx context.Context, key string) string {
if nil == ctx {
return ""
}
if ginCtx, ok := ctx.(*gin.Context); ok {
return ginRequest.WrapperHandle.GetCtxStringData(ginCtx, key, "")
}
val := ctx.Value(key)
if nil == val {
return ""
}
return fmt.Sprintf("%v", val)
}
// NewLogData ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:21 2024/7/23
func NewLogData(logType string, code string, logData map[string]any) *LogData {
if logData == nil {
logData = make(map[string]any)
}
func NewLogData(ctx context.Context, logType string, code string, logData map[string]any) *LogData {
hostname, _ := os.Hostname()
commonLogData := &LogData{
Uri: "",
TraceID: "",
UserID: "",
UserRoleID: "",
OperateMode: "",
Uri: getStrVal(ctx, "uri"),
TraceID: getStrVal(ctx, "trace_id"),
UserID: getStrVal(ctx, "user_id"),
UserRoleID: getStrVal(ctx, "user_role_id"),
OperateMode: getStrVal(ctx, "operate_mode"),
LogType: logType,
CodeVersion: "",
ServiceVersion: "",
ClientIp: "",
CodeVersion: getStrVal(ctx, "code_version"),
ServiceVersion: getStrVal(ctx, "service_version"),
ClientIp: getStrVal(ctx, "client_ip"),
ServerIp: util.IP.GetHostIP(),
Hostname: hostname,
Code: code,
Data: logData,
}
GetFillLogDataFunc()(commonLogData)
return commonLogData
}