logger/util.go

68 lines
1.5 KiB
Go

// 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
func NewLogData(logType string, code string, logData map[string]any) *LogData {
if logData == nil {
logData = make(map[string]any)
}
hostname, _ := os.Hostname()
commonLogData := &LogData{
Uri: "",
TraceID: "",
UserID: "",
UserRoleID: "",
OperateMode: "",
LogType: logType,
CodeVersion: "",
ServiceVersion: "",
ClientIp: "",
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(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
}