From c315db9a8a9a7788173197d8a17af290314b5005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 21 Jun 2024 18:48:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=89=A9=E5=B1=95=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/IRedisClient.go | 2 +- client.go | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/abstract/IRedisClient.go b/abstract/IRedisClient.go index 3dce1b4..1b3a8a8 100644 --- a/abstract/IRedisClient.go +++ b/abstract/IRedisClient.go @@ -25,7 +25,7 @@ type IRedisClient interface { GetRealClientWithError(instanceFlag string) (*redis.Client, error) // 获取带error的客户端连接 AddClient(instanceFlag string, instanceConfig *define.Options) error // 添加新的客户端连接 RemoveClient(instanceFlag string) // 移除一个客户端连接 - SetLogger(loggerInstance *zap.Logger) // 设置日志实例, 全局生效, 而非针对某一个实例 + SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) // 设置日志实例, 全局生效, 而非针对某一个实例 Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult // 执行任意命令 SetCommandWhiteList(command []string) // 设置命令的白名单,全局生效, 而非单独针对某一个实例 } diff --git a/client.go b/client.go index 23adad2..e1258f8 100644 --- a/client.go +++ b/client.go @@ -38,6 +38,7 @@ type OwnClient struct { instanceTable map[string]*redisClient.Client whiteCommandTable map[string]bool logger *zap.Logger + extraLogFieldList []string } func (o *OwnClient) isAllowCommand(command string) bool { @@ -61,6 +62,9 @@ func (o *OwnClient) isAllowCommand(command string) bool { // // Date : 11:05 2024/6/19 func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult { + if nil == ctx { + ctx = context.Background() + } var ( instance *redisClient.Client ) @@ -90,8 +94,7 @@ func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command strin // 未注入日志实例 return } - o.logger.Info( - "Redis命令执行记录", + logDataList := []zap.Field{ zap.Int64("start_time", res.StartTime), zap.Int64("finish_time", res.FinishTime), zap.Int64("used_time", res.UsedTime), @@ -99,6 +102,13 @@ func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command strin zap.String("arg_list", strings.Join(res.ArgList, " ")), zap.String("execute_result", res.Result), zap.Error(res.Err), + } + for _, item := range o.extraLogFieldList { + logDataList = append(logDataList, zap.Any(item, ctx.Value(item))) + } + o.logger.Info( + "Redis命令执行记录", + logDataList..., ) }() if instance, res.Err = o.GetRealClientWithError(instanceFlag); nil != res.Err { @@ -189,6 +199,7 @@ func (o *OwnClient) RemoveClient(instanceFlag string) { delete(o.instanceTable, instanceFlag) } -func (o *OwnClient) SetLogger(loggerInstance *zap.Logger) { +func (o *OwnClient) SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) { o.logger = loggerInstance + o.extraLogFieldList = extraLogFieldList }