From 77de4c7fd91cea11c42728aeee715b02a1fd84a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 24 Jul 2024 21:41:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E6=97=A5=E5=BF=97=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 57 ++++++++++++++++++++++++++++++++++++++++++++- go.mod | 2 +- go.sum | 2 ++ util.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 util.go diff --git a/define.go b/define.go index 9ee731d..91774bb 100644 --- a/define.go +++ b/define.go @@ -18,7 +18,17 @@ import ( "go.uber.org/zap/zapcore" ) -var wsLoggerConnect storage.IConnection // ws 日志连接管理实例 +// FillLogDataFunc 定义填充数据的方法 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:33 2024/7/24 +type FillLogDataFunc func(data *LogData) + +var ( + wsLoggerConnect storage.IConnection // ws 日志连接管理实例 + fillLogDataFunc FillLogDataFunc // 填充数据方法 +) // SetWsLoggConnect 设置ws connect // @@ -38,6 +48,51 @@ func GetWsLoggConnect(connect storage.IConnection) { 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 记录日志数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:07 2024/7/23 +type LogData struct { + Env string `json:"env"` // 运行环境 + Uri string `json:"uri"` // 请求的接口 + TraceID string `json:"trace_id"` // 请求的trace_id + UserID string `json:"user_id"` // 用户ID + UserRoleID string `json:"user_role_id"` // 用户角色 + OperateMode string `json:"operate_mode"` // 操作模式 + LogType string `json:"log_type"` // 日志类型 + CodeVersion string `json:"code_version"` // 代码版本 + ServiceVersion string `json:"service_version"` // 服务本身的版本 + ClientIp string `json:"client_ip"` // 客户端IP + ServerIp string `json:"server_ip"` // 服务器IP + Hostname string `json:"hostname"` // 服务器主机名 + Code string `json:"code"` // 日志状态码 + Data map[string]any `json:"data"` // 扩展记录的数据, 会展开一层进行记录 +} + // InputLogConfig 输入的日志配置 // // Author : go_developer@163.com<白茶清欢> diff --git a/go.mod b/go.mod index 5ebd6dc..4321a19 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.22.5 require ( git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240723085016-ee0510753552 - git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230826052425-e7c13b6089ca + git.zhangdeman.cn/zhangdeman/gin v0.0.0-20240723135300-86cecc4eb707 git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20240723075210-85feada512b2 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 1165155..c9bce85 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOs git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230826052425-e7c13b6089ca h1:dih0pGEf2snCN7QImFSV62i7jNDKm/kOy/nC4pN9rQI= git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230826052425-e7c13b6089ca/go.mod h1:0qCJAZU+tDC11ZDe2rh8gOfYQhWKlvutNAbs5i4eGfo= +git.zhangdeman.cn/zhangdeman/gin v0.0.0-20240723135300-86cecc4eb707 h1:Kp56iXcOBz9a6OdqEHWDNOzSH/t7vAtECvvbiHa4r0w= +git.zhangdeman.cn/zhangdeman/gin v0.0.0-20240723135300-86cecc4eb707/go.mod h1:L9w2KH8amtv0+OG5G5etEEahmxmNqdDlGML438b/jXQ= git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442 h1:1eBf0C0gdpBQOqjTK3UCw/mwzQ/SCodx3iTQtidx9eE= git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442/go.mod h1:hFYWiS+ExIuJJJdwHWy4P3pVHbd/0mpv53qlbhDNdTI= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y= diff --git a/util.go b/util.go new file mode 100644 index 0000000..fcd6f0c --- /dev/null +++ b/util.go @@ -0,0 +1,69 @@ +// Package logger ... +// +// Description : logger ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-07-24 21:14 +package logger + +import ( + "os" + + ginRequest "git.zhangdeman.cn/zhangdeman/gin/request" + "git.zhangdeman.cn/zhangdeman/network/util" + "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +// NewLogData ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:21 2024/7/23 +func NewLogData(ctx *gin.Context, logType string, code string, logData map[string]any) *LogData { + if logData == nil { + logData = make(map[string]any) + } + hostname, _ := os.Hostname() + commonLogData := &LogData{ + Uri: ginRequest.WrapperHandle.GetUri(ctx, ""), + TraceID: "", + UserID: "", + UserRoleID: "", + OperateMode: "", + LogType: logType, + CodeVersion: "", + ServiceVersion: "", + ClientIp: ginRequest.WrapperHandle.GetClientIp(ctx, "0.0.0.0"), + ServerIp: util.IP.GetHostIP(), + Hostname: hostname, + Code: code, + Data: logData, + } + GetFillLogDataFunc()(commonLogData) + return commonLogData +} + +// ZapLogDataList 记录的日志数据字段列表 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:33 2024/7/23 +func ZapLogDataList(ctx *gin.Context, logData *LogData) []zap.Field { + logDataList := make([]zap.Field, 0) + if logData == nil { + return logDataList + } + var mapData map[string]any + _ = serialize.JSON.Transition(logData, &mapData) + delete(mapData, "data") // data字段单独展开 + for k, v := range mapData { + logDataList = append(logDataList, zap.Any(k, v)) + } + for k, v := range logData.Data { // 展开一层数据属性 + logDataList = append(logDataList, zap.Any("data_"+k, v)) + } + return logDataList +}