基础日志数据结构
This commit is contained in:
parent
2feb4d661c
commit
77de4c7fd9
57
define.go
57
define.go
@ -18,7 +18,17 @@ import (
|
|||||||
"go.uber.org/zap/zapcore"
|
"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
|
// SetWsLoggConnect 设置ws connect
|
||||||
//
|
//
|
||||||
@ -38,6 +48,51 @@ 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 记录日志数据
|
||||||
|
//
|
||||||
|
// 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 输入的日志配置
|
// InputLogConfig 输入的日志配置
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
2
go.mod
2
go.mod
@ -6,7 +6,7 @@ toolchain go1.22.5
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240723085016-ee0510753552
|
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
|
git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20240723075210-85feada512b2
|
||||||
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
|
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
|
2
go.sum
2
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/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 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-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 h1:1eBf0C0gdpBQOqjTK3UCw/mwzQ/SCodx3iTQtidx9eE=
|
||||||
git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442/go.mod h1:hFYWiS+ExIuJJJdwHWy4P3pVHbd/0mpv53qlbhDNdTI=
|
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=
|
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
|
||||||
|
69
util.go
Normal file
69
util.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user