1
0
mirror of https://github.com/redis/go-redis.git synced 2025-12-02 06:22:31 +03:00

feat: add optional logger wherever possible

This commit introduces an optional logger parameter to various structs.
This enhancement allows users to provide custom logging implementations.
This commit is contained in:
ccoVeille
2025-10-24 14:59:40 +02:00
parent 5b0b228a37
commit c98107019e
15 changed files with 340 additions and 172 deletions

13
ring.go
View File

@@ -154,6 +154,8 @@ type RingOptions struct {
DisableIdentity bool
IdentitySuffix string
UnstableResp3 bool
Logger internal.LoggerWithLevel
}
func (opt *RingOptions) init() {
@@ -345,7 +347,7 @@ func (c *ringSharding) SetAddrs(addrs map[string]string) {
cleanup := func(shards map[string]*ringShard) {
for addr, shard := range shards {
if err := shard.Client.Close(); err != nil {
internal.Logger.Printf(context.Background(), "shard.Close %s failed: %s", addr, err)
c.logger().Errorf(context.Background(), "shard.Close %s failed: %s", addr, err)
}
}
}
@@ -490,7 +492,7 @@ func (c *ringSharding) Heartbeat(ctx context.Context, frequency time.Duration) {
for _, shard := range c.List() {
isUp := c.opt.HeartbeatFn(ctx, shard.Client)
if shard.Vote(isUp) {
internal.Logger.Printf(ctx, "ring shard state changed: %s", shard)
c.logger().Infof(ctx, "ring shard state changed: %s", shard)
rebalance = true
}
}
@@ -559,6 +561,13 @@ func (c *ringSharding) Close() error {
return firstErr
}
func (c *ringSharding) logger() internal.LoggerWithLevel {
if c.opt.Logger != nil {
return c.opt.Logger
}
return internal.LegacyLoggerWithLevel
}
//------------------------------------------------------------------------------
// Ring is a Redis client that uses consistent hashing to distribute