优化日志数据生成

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

View File

@ -18,16 +18,8 @@ import (
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
// FillLogDataFunc 定义填充数据的方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:33 2024/7/24
type FillLogDataFunc func(data *LogData)
var ( var (
wsLoggerConnect storage.IConnection // ws 日志连接管理实例 wsLoggerConnect storage.IConnection // ws 日志连接管理实例
fillLogDataFunc FillLogDataFunc // 填充数据方法
) )
// SetWsLoggConnect 设置ws connect // SetWsLoggConnect 设置ws connect
@ -48,29 +40,6 @@ func GetWsLoggConnect(connect storage.IConnection) {
wsLoggerConnect = connect wsLoggerConnect = connect
} }
// SetFillLogDataFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:34 2024/7/24
func SetFillLogDataFunc(fillFunc FillLogDataFunc) {
fillLogDataFunc = fillFunc
}
// GetFillLogDataFunc 获取填充数据的方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:36 2024/7/24
func GetFillLogDataFunc() FillLogDataFunc {
if nil == fillLogDataFunc {
return func(data *LogData) {
}
}
return fillLogDataFunc
}
// LogData 记录日志数据 // LogData 记录日志数据
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>

41
util.go
View File

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

View File

@ -191,7 +191,7 @@ func (g *Gorm) write(ctx context.Context, message string, level string, data map
} }
} }
dataList := logger2.ZapLogDataList(logger2.NewLogData(consts.LogTypeDatabase, "", data)) dataList := logger2.ZapLogDataList(logger2.NewLogData(g.outCtx, consts.LogTypeDatabase, "", data))
switch strings.ToUpper(level) { switch strings.ToUpper(level) {
case consts.LogLevelDebug: case consts.LogLevelDebug:
g.instance.Debug(message, dataList...) g.instance.Debug(message, dataList...)