logger/util.go

70 lines
1.7 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"
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
}