优化日志数据结构以及数据构建 #6
73
define.go
73
define.go
@ -8,10 +8,16 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/network/util"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/op_any"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/websocket/storage"
|
||||
"go.uber.org/zap"
|
||||
@ -41,32 +47,36 @@ func GetWsLoggConnect() storage.IConnection {
|
||||
}
|
||||
|
||||
// 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"` // 操作模式
|
||||
OperateMode string `json:"operate_mode"` // 操作模式(PC/APP/PAD等)
|
||||
LogType string `json:"log_type"` // 日志类型
|
||||
CodeVersion string `json:"code_version"` // 代码版本
|
||||
CodeVersion string `json:"code_version"` // 代码版本(可以设置为git commit id)
|
||||
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"` // 日志状态码
|
||||
Code string `json:"code"` // 日志分类标记码
|
||||
Data map[string]any `json:"data"` // 扩展记录的数据, 会展开一层进行记录
|
||||
}
|
||||
|
||||
// ToFieldList 转换为 zap.Field 列表
|
||||
func (ld *LogData) ToFieldList() []zap.Field {
|
||||
var (
|
||||
fieldList []zap.Field
|
||||
mapLogData map[string]any
|
||||
)
|
||||
serialize.JSON.TransitionIgnoreError(ld, &mapLogData)
|
||||
for k, v := range mapLogData {
|
||||
fieldList = append(fieldList, zap.Any(k, v))
|
||||
}
|
||||
return fieldList
|
||||
}
|
||||
|
||||
// InputLogConfig 输入的日志配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:40 2022/6/12
|
||||
type InputLogConfig struct {
|
||||
Name string `json:"name" yaml:"name"` // 日志文件名
|
||||
Path string `json:"path" yaml:"path"` // 日志文件路径
|
||||
@ -86,6 +96,45 @@ type InputLogConfig struct {
|
||||
ZincSyncConfig *ZincConfig `json:"zinc_sync_config" yaml:"zinc_sync_config"` // 日志同步至zinc的配置
|
||||
}
|
||||
|
||||
// NewLogData ...
|
||||
func NewLogData(ctx context.Context, logType string, code string, logData map[string]any) *LogData {
|
||||
hostname, _ := os.Hostname()
|
||||
if nil == ctx {
|
||||
ctx = context.Background()
|
||||
}
|
||||
commonLogData := &LogData{
|
||||
Env: getStrVal(ctx, consts.GinEnvField),
|
||||
Uri: getStrVal(ctx, consts.GinRequestURIField),
|
||||
TraceID: getStrVal(ctx, consts.GinTraceIDField),
|
||||
UserID: getStrVal(ctx, consts.GinUserIDField),
|
||||
OperateMode: getStrVal(ctx, consts.GinOperateModeField),
|
||||
LogType: logType,
|
||||
CodeVersion: getStrVal(ctx, consts.GinCodeVersionField),
|
||||
ServiceVersion: getStrVal(ctx, consts.GinServiceVersionField),
|
||||
ClientIp: getStrVal(ctx, consts.GinClientIpField),
|
||||
ServerIp: util.IP.GetHostIP(),
|
||||
Hostname: hostname,
|
||||
Code: code,
|
||||
Data: logData,
|
||||
}
|
||||
return commonLogData
|
||||
}
|
||||
|
||||
func getStrVal(ctx context.Context, key string) string {
|
||||
val := ctx.Value(key)
|
||||
if nil != val {
|
||||
return op_any.AnyDataType(val).ToString()
|
||||
}
|
||||
if v := ctx.Value(consts.GinContextDataField); nil != v {
|
||||
if data, ok := v.(map[string]any); ok {
|
||||
if searchVal, exist := data[key]; exist && nil != searchVal {
|
||||
return fmt.Sprintf("%v", searchVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetLogInstanceFromInputConfig 从输入配置获取日志实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
||||
82
util.go
82
util.go
@ -1,82 +0,0 @@
|
||||
// Package logger ...
|
||||
//
|
||||
// Description : logger ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-07-24 21:14
|
||||
package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/network/util"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/op_any"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func getStrVal(ctx context.Context, key string) string {
|
||||
val := ctx.Value(key)
|
||||
if nil != val {
|
||||
return op_any.AnyDataType(val).ToString()
|
||||
}
|
||||
if v := ctx.Value(consts.GinContextDataField); nil != v {
|
||||
if data, ok := v.(map[string]any); ok {
|
||||
if searchVal, exist := data[key]; exist && nil != searchVal {
|
||||
return fmt.Sprintf("%v", searchVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// NewLogData ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:21 2024/7/23
|
||||
func NewLogData(ctx context.Context, logType string, code string, logData map[string]any) *LogData {
|
||||
hostname, _ := os.Hostname()
|
||||
if nil == ctx {
|
||||
ctx = context.Background()
|
||||
}
|
||||
commonLogData := &LogData{
|
||||
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, consts.GinCodeVersionField),
|
||||
ServiceVersion: getStrVal(ctx, consts.GinServiceVersionField),
|
||||
ClientIp: getStrVal(ctx, consts.GinClientIpField),
|
||||
ServerIp: util.IP.GetHostIP(),
|
||||
Hostname: hostname,
|
||||
Code: code,
|
||||
Data: logData,
|
||||
}
|
||||
return commonLogData
|
||||
}
|
||||
|
||||
// ZapLogDataList 记录的日志数据字段列表
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:33 2024/7/23
|
||||
func ZapLogDataList(logData *LogData) []zap.Field {
|
||||
logDataList := make([]zap.Field, 0)
|
||||
if logData == nil {
|
||||
return logDataList
|
||||
}
|
||||
var mapData map[string]any
|
||||
_ = serialize.JSON.Transition(logData, &mapData)
|
||||
for k, v := range mapData {
|
||||
logDataList = append(logDataList, zap.Any(k, v))
|
||||
}
|
||||
return logDataList
|
||||
}
|
||||
Reference in New Issue
Block a user