81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
// Package log ...
|
|
//
|
|
// Description : httpclient ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2025-03-31 18:02
|
|
package log
|
|
|
|
import (
|
|
"context"
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
|
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var (
|
|
logger *zap.Logger
|
|
)
|
|
|
|
// Set 设置日志实例
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:02 2025/3/31
|
|
func Set(l *zap.Logger) {
|
|
logger = l
|
|
}
|
|
|
|
// Get 获取日志实例
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:19 2025/3/31
|
|
func Get() *zap.Logger {
|
|
return logger
|
|
}
|
|
|
|
var buildHttpLogDataFunc BuildHttpLogDataFunc
|
|
|
|
// BuildHttpLogDataFunc 构建http请求日志数据的方法
|
|
type BuildHttpLogDataFunc func(ctx context.Context, reqCfg *define.Request, response *define.Response) []zap.Field
|
|
|
|
func SetBuildDataFunc(f BuildHttpLogDataFunc) {
|
|
buildHttpLogDataFunc = f
|
|
}
|
|
|
|
func GetBuildDataFunc() BuildHttpLogDataFunc {
|
|
return buildHttpLogDataFunc
|
|
}
|
|
|
|
// Record 日志记录
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:07 2025/3/31
|
|
func Record(ctx context.Context, logInstance *zap.Logger, level consts.LogLevel, msg string, reqCfg *define.Request, response *define.Response) {
|
|
if nil == logInstance {
|
|
// 未设置日志实例
|
|
return
|
|
}
|
|
buildDataFunc := GetBuildDataFunc()
|
|
if nil == buildDataFunc {
|
|
// 未设置构建日志数据的方法
|
|
return
|
|
}
|
|
fields := buildDataFunc(ctx, reqCfg, response)
|
|
switch level {
|
|
case consts.LogLevelDebug:
|
|
logInstance.Debug(msg, fields...)
|
|
case consts.LogLevelInfo:
|
|
logInstance.Info(msg, fields...)
|
|
case consts.LogLevelWarn:
|
|
logInstance.Warn(msg, fields...)
|
|
case consts.LogLevelError:
|
|
logInstance.Error(msg, fields...)
|
|
case consts.LogLevelPanic:
|
|
logInstance.Panic(msg, fields...)
|
|
}
|
|
}
|