From d3ee281748d0ffc71c4c9d36e9c3f7d6eecdde2b Mon Sep 17 00:00:00 2001 From: Francisco Souza Date: Wed, 3 Feb 2016 12:30:39 -0500 Subject: [PATCH] Declare and use a package-level Logger This allow users of the API to override the Logger. Fix #250. --- cluster.go | 5 ++--- commands.go | 5 ++--- multi.go | 5 ++--- pool.go | 7 +++---- pubsub.go | 7 +++---- redis.go | 5 ++++- ring.go | 3 +-- sentinel.go | 27 +++++++++++++-------------- 8 files changed, 30 insertions(+), 34 deletions(-) diff --git a/cluster.go b/cluster.go index 7291cb3..817cb90 100644 --- a/cluster.go +++ b/cluster.go @@ -1,7 +1,6 @@ package redis import ( - "log" "math/rand" "sync" "sync/atomic" @@ -256,13 +255,13 @@ func (c *ClusterClient) reloadSlots() { client, err := c.randomClient() if err != nil { - log.Printf("redis: randomClient failed: %s", err) + Logger.Printf("redis: randomClient failed: %s", err) return } slots, err := client.ClusterSlots().Result() if err != nil { - log.Printf("redis: ClusterSlots failed: %s", err) + Logger.Printf("redis: ClusterSlots failed: %s", err) return } c.setSlots(slots) diff --git a/commands.go b/commands.go index f8a80f5..a3495ee 100644 --- a/commands.go +++ b/commands.go @@ -2,7 +2,6 @@ package redis import ( "io" - "log" "strconv" "time" ) @@ -32,7 +31,7 @@ func usePrecise(dur time.Duration) bool { func formatMs(dur time.Duration) string { if dur > 0 && dur < time.Millisecond { - log.Printf( + Logger.Printf( "redis: specified duration is %s, but minimal supported value is %s", dur, time.Millisecond, ) @@ -42,7 +41,7 @@ func formatMs(dur time.Duration) string { func formatSec(dur time.Duration) string { if dur > 0 && dur < time.Second { - log.Printf( + Logger.Printf( "redis: specified duration is %s, but minimal supported value is %s", dur, time.Second, ) diff --git a/multi.go b/multi.go index 0e0281d..498951e 100644 --- a/multi.go +++ b/multi.go @@ -3,7 +3,6 @@ package redis import ( "errors" "fmt" - "log" ) var errDiscard = errors.New("redis: Discard can be used only inside Exec") @@ -53,7 +52,7 @@ func (c *Multi) putConn(cn *conn, err error) { } else { err := c.base.connPool.Put(cn) if err != nil { - log.Printf("redis: putConn failed: %s", err) + Logger.Printf("redis: putConn failed: %s", err) } } } @@ -70,7 +69,7 @@ func (c *Multi) process(cmd Cmder) { func (c *Multi) Close() error { c.closed = true if err := c.Unwatch().Err(); err != nil { - log.Printf("redis: Unwatch failed: %s", err) + Logger.Printf("redis: Unwatch failed: %s", err) } return c.base.Close() } diff --git a/pool.go b/pool.go index 76ba10d..af7773f 100644 --- a/pool.go +++ b/pool.go @@ -3,7 +3,6 @@ package redis import ( "errors" "fmt" - "log" "sync" "sync/atomic" "time" @@ -178,7 +177,7 @@ func (p *connPool) First() *conn { var err error cn, err = p.replace(cn) if err != nil { - log.Printf("redis: replace failed: %s", err) + Logger.Printf("redis: replace failed: %s", err) continue } } @@ -200,7 +199,7 @@ func (p *connPool) wait() *conn { var err error cn, err = p.replace(cn) if err != nil { - log.Printf("redis: replace failed: %s", err) + Logger.Printf("redis: replace failed: %s", err) continue } } @@ -272,7 +271,7 @@ func (p *connPool) Put(cn *conn) error { if cn.rd.Buffered() != 0 { b, _ := cn.rd.Peek(cn.rd.Buffered()) err := fmt.Errorf("redis: connection has unread data: %q", b) - log.Print(err) + Logger.Print(err) return p.Remove(cn, err) } if p.opt.getIdleTimeout() > 0 { diff --git a/pubsub.go b/pubsub.go index 031eb84..6baaeaf 100644 --- a/pubsub.go +++ b/pubsub.go @@ -2,7 +2,6 @@ package redis import ( "fmt" - "log" "net" "time" ) @@ -239,12 +238,12 @@ func (c *PubSub) reconnect(reason error) { if len(c.channels) > 0 { if err := c.Subscribe(c.channels...); err != nil { - log.Printf("redis: Subscribe failed: %s", err) + Logger.Printf("redis: Subscribe failed: %s", err) } } if len(c.patterns) > 0 { if err := c.PSubscribe(c.patterns...); err != nil { - log.Printf("redis: PSubscribe failed: %s", err) + Logger.Printf("redis: PSubscribe failed: %s", err) } } } @@ -269,7 +268,7 @@ func (c *PubSub) ReceiveMessage() (*Message, error) { if err == nil { continue } - log.Printf("redis: PubSub.Ping failed: %s", err) + Logger.Printf("redis: PubSub.Ping failed: %s", err) } } diff --git a/redis.go b/redis.go index db22af6..aac6fbe 100644 --- a/redis.go +++ b/redis.go @@ -4,9 +4,12 @@ import ( "fmt" "log" "net" + "os" "time" ) +var Logger = log.New(os.Stderr, "", log.LstdFlags) + type baseClient struct { connPool pool opt *Options @@ -27,7 +30,7 @@ func (c *baseClient) putConn(cn *conn, err error) { err = c.connPool.Put(cn) } if err != nil { - log.Printf("redis: putConn failed: %s", err) + Logger.Printf("redis: putConn failed: %s", err) } } diff --git a/ring.go b/ring.go index d6bcf80..1601221 100644 --- a/ring.go +++ b/ring.go @@ -3,7 +3,6 @@ package redis import ( "errors" "fmt" - "log" "sync" "time" @@ -202,7 +201,7 @@ func (ring *Ring) heartbeat() { for _, shard := range ring.shards { err := shard.Client.Ping().Err() if shard.Vote(err == nil || err == errPoolTimeout) { - log.Printf("redis: ring shard state changed: %s", shard) + Logger.Printf("redis: ring shard state changed: %s", shard) rebalance = true } } diff --git a/sentinel.go b/sentinel.go index 7edd75f..462c9d3 100644 --- a/sentinel.go +++ b/sentinel.go @@ -3,7 +3,6 @@ package redis import ( "errors" "fmt" - "log" "net" "strings" "sync" @@ -145,11 +144,11 @@ func (d *sentinelFailover) MasterAddr() (string, error) { if d._sentinel != nil { addr, err := d._sentinel.GetMasterAddrByName(d.masterName).Result() if err != nil { - log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) + Logger.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) d.resetSentinel() } else { addr := net.JoinHostPort(addr[0], addr[1]) - log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr) + Logger.Printf("redis-sentinel: %q addr is %s", d.masterName, addr) return addr, nil } } @@ -168,7 +167,7 @@ func (d *sentinelFailover) MasterAddr() (string, error) { }) masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result() if err != nil { - log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) + Logger.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) sentinel.Close() continue } @@ -178,7 +177,7 @@ func (d *sentinelFailover) MasterAddr() (string, error) { d.setSentinel(sentinel) addr := net.JoinHostPort(masterAddr[0], masterAddr[1]) - log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr) + Logger.Printf("redis-sentinel: %q addr is %s", d.masterName, addr) return addr, nil } @@ -194,7 +193,7 @@ func (d *sentinelFailover) setSentinel(sentinel *sentinelClient) { func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) { sentinels, err := sentinel.Sentinels(d.masterName).Result() if err != nil { - log.Printf("redis-sentinel: Sentinels %q failed: %s", d.masterName, err) + Logger.Printf("redis-sentinel: Sentinels %q failed: %s", d.masterName, err) return } for _, sentinel := range sentinels { @@ -204,7 +203,7 @@ func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) { if key == "name" { sentinelAddr := vals[i+1].(string) if !contains(d.sentinelAddrs, sentinelAddr) { - log.Printf( + Logger.Printf( "redis-sentinel: discovered new %q sentinel: %s", d.masterName, sentinelAddr, ) @@ -232,7 +231,7 @@ func (d *sentinelFailover) closeOldConns(newMaster string) { "redis-sentinel: closing connection to the old master %s", cn.RemoteAddr(), ) - log.Print(err) + Logger.Print(err) d.pool.Remove(cn, err) } else { cnsToPut = append(cnsToPut, cn) @@ -250,7 +249,7 @@ func (d *sentinelFailover) listen() { if pubsub == nil { pubsub = d._sentinel.PubSub() if err := pubsub.Subscribe("+switch-master"); err != nil { - log.Printf("redis-sentinel: Subscribe failed: %s", err) + Logger.Printf("redis-sentinel: Subscribe failed: %s", err) d.lock.Lock() d.resetSentinel() d.lock.Unlock() @@ -260,7 +259,7 @@ func (d *sentinelFailover) listen() { msgIface, err := pubsub.Receive() if err != nil { - log.Printf("redis-sentinel: Receive failed: %s", err) + Logger.Printf("redis-sentinel: Receive failed: %s", err) pubsub.Close() return } @@ -271,23 +270,23 @@ func (d *sentinelFailover) listen() { case "+switch-master": parts := strings.Split(msg.Payload, " ") if parts[0] != d.masterName { - log.Printf("redis-sentinel: ignore new %s addr", parts[0]) + Logger.Printf("redis-sentinel: ignore new %s addr", parts[0]) continue } addr := net.JoinHostPort(parts[3], parts[4]) - log.Printf( + Logger.Printf( "redis-sentinel: new %q addr is %s", d.masterName, addr, ) d.closeOldConns(addr) default: - log.Printf("redis-sentinel: unsupported message: %s", msg) + Logger.Printf("redis-sentinel: unsupported message: %s", msg) } case *Subscription: // Ignore. default: - log.Printf("redis-sentinel: unsupported message: %s", msgIface) + Logger.Printf("redis-sentinel: unsupported message: %s", msgIface) } } }