logger/util.go

68 lines
1.5 KiB
Go
Raw Normal View History

2024-07-24 21:41:07 +08:00
// Package logger ...
//
// Description : logger ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-07-24 21:14
package logger
import (
"os"
"git.zhangdeman.cn/zhangdeman/network/util"
"git.zhangdeman.cn/zhangdeman/serialize"
"go.uber.org/zap"
)
// NewLogData ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:21 2024/7/23
2024-07-24 22:03:22 +08:00
func NewLogData(logType string, code string, logData map[string]any) *LogData {
2024-07-24 21:41:07 +08:00
if logData == nil {
logData = make(map[string]any)
}
hostname, _ := os.Hostname()
commonLogData := &LogData{
2024-07-24 22:03:22 +08:00
Uri: "",
2024-07-24 21:41:07 +08:00
TraceID: "",
UserID: "",
UserRoleID: "",
OperateMode: "",
LogType: logType,
CodeVersion: "",
ServiceVersion: "",
2024-07-24 22:03:22 +08:00
ClientIp: "",
2024-07-24 21:41:07 +08:00
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
2024-07-24 22:03:22 +08:00
func ZapLogDataList(logData *LogData) []zap.Field {
2024-07-24 21:41:07 +08:00
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
}