upgrade: 升级日志的实现, 不在依赖 gin, 通用化

This commit is contained in:
2025-08-09 15:18:44 +08:00
parent ad9ce47b38
commit abad42c2cb
5 changed files with 26 additions and 115 deletions

63
util.go
View File

@ -9,55 +9,21 @@ package logger
import (
"context"
"fmt"
"os"
"github.com/gin-gonic/gin"
ginRequest "git.zhangdeman.cn/zhangdeman/gin/request"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/network/util"
"git.zhangdeman.cn/zhangdeman/serialize"
"git.zhangdeman.cn/zhangdeman/wrapper"
"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)
}
// 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
return wrapper.AnyDataType(val).ToString().Value()
}
// NewLogData ...
@ -68,22 +34,19 @@ func NewLogDataForGin(ctx *gin.Context, logType string, code string, logData map
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)
ctx = context.Background()
}
commonLogData := &LogData{
Env: getStrVal(ctx, "env"),
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"),
Env: getStrVal(ctx, consts.GinEnvField),
Uri: getStrVal(ctx, consts.GinRequestURIField),
TraceID: getStrVal(ctx, consts.GinTraceIDField),
UserID: getStrVal(ctx, consts.GinUserIDField),
UserRoleID: getStrVal(ctx, consts.GinUserRoleIDField),
OperateMode: getStrVal(ctx, consts.GinOperateModeField),
LogType: logType,
CodeVersion: getStrVal(ctx, "code_version"),
ServiceVersion: getStrVal(ctx, "service_version"),
ClientIp: getStrVal(ctx, "client_ip"),
CodeVersion: getStrVal(ctx, consts.GinCodeVersionField),
ServiceVersion: getStrVal(ctx, consts.GinServiceVersionField),
ClientIp: getStrVal(ctx, consts.GinClientIpField),
ServerIp: util.IP.GetHostIP(),
Hostname: hostname,
Code: code,