Move logging to internal package.

This commit is contained in:
Vladimir Mihailenco 2016-04-09 13:54:44 +03:00
parent 4fba0dda91
commit 8679174a1b
9 changed files with 59 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"gopkg.in/redis.v3/internal"
"gopkg.in/redis.v3/internal/hashtag" "gopkg.in/redis.v3/internal/hashtag"
"gopkg.in/redis.v3/internal/pool" "gopkg.in/redis.v3/internal/pool"
) )
@ -269,13 +270,13 @@ func (c *ClusterClient) reloadSlots() {
client, err := c.randomClient() client, err := c.randomClient()
if err != nil { if err != nil {
Logger.Printf("randomClient failed: %s", err) internal.Logf("randomClient failed: %s", err)
return return
} }
slots, err := client.ClusterSlots().Result() slots, err := client.ClusterSlots().Result()
if err != nil { if err != nil {
Logger.Printf("ClusterSlots failed: %s", err) internal.Logf("ClusterSlots failed: %s", err)
return return
} }
c.setSlots(slots) c.setSlots(slots)
@ -302,14 +303,14 @@ func (c *ClusterClient) reaper(frequency time.Duration) {
for _, client := range c.getClients() { for _, client := range c.getClients() {
nn, err := client.connPool.(*pool.ConnPool).ReapStaleConns() nn, err := client.connPool.(*pool.ConnPool).ReapStaleConns()
if err != nil { if err != nil {
Logger.Printf("ReapStaleConns failed: %s", err) internal.Logf("ReapStaleConns failed: %s", err)
} else { } else {
n += nn n += nn
} }
} }
s := c.PoolStats() s := c.PoolStats()
Logger.Printf( internal.Logf(
"reaper: removed %d stale conns (TotalConns=%d FreeConns=%d Requests=%d Hits=%d Timeouts=%d)", "reaper: removed %d stale conns (TotalConns=%d FreeConns=%d Requests=%d Hits=%d Timeouts=%d)",
n, s.TotalConns, s.FreeConns, s.Requests, s.Hits, s.Timeouts, n, s.TotalConns, s.FreeConns, s.Requests, s.Hits, s.Timeouts,
) )

View File

@ -4,6 +4,8 @@ import (
"io" "io"
"strconv" "strconv"
"time" "time"
"gopkg.in/redis.v3/internal"
) )
func formatInt(i int64) string { func formatInt(i int64) string {
@ -31,7 +33,7 @@ func usePrecise(dur time.Duration) bool {
func formatMs(dur time.Duration) string { func formatMs(dur time.Duration) string {
if dur > 0 && dur < time.Millisecond { if dur > 0 && dur < time.Millisecond {
Logger.Printf( internal.Logf(
"specified duration is %s, but minimal supported value is %s", "specified duration is %s, but minimal supported value is %s",
dur, time.Millisecond, dur, time.Millisecond,
) )
@ -41,7 +43,7 @@ func formatMs(dur time.Duration) string {
func formatSec(dur time.Duration) string { func formatSec(dur time.Duration) string {
if dur > 0 && dur < time.Second { if dur > 0 && dur < time.Second {
Logger.Printf( internal.Logf(
"specified duration is %s, but minimal supported value is %s", "specified duration is %s, but minimal supported value is %s",
dur, time.Second, dur, time.Second,
) )

22
internal/log.go Normal file
View File

@ -0,0 +1,22 @@
package internal
import (
"fmt"
"io/ioutil"
"log"
)
var Debug bool
var Logger = log.New(ioutil.Discard, "redis: ", log.LstdFlags)
func Debugf(s string, args ...interface{}) {
if !Debug {
return
}
Logger.Output(2, fmt.Sprintf(s, args...))
}
func Logf(s string, args ...interface{}) {
Logger.Output(2, fmt.Sprintf(s, args...))
}

View File

@ -3,17 +3,15 @@ package pool
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log"
"net" "net"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"gopkg.in/bsm/ratelimit.v1" "gopkg.in/bsm/ratelimit.v1"
)
var Logger = log.New(ioutil.Discard, "redis: ", log.LstdFlags) "gopkg.in/redis.v3/internal"
)
var ( var (
ErrClosed = errors.New("redis: client is closed") ErrClosed = errors.New("redis: client is closed")
@ -212,7 +210,7 @@ func (p *ConnPool) Put(cn *Conn) error {
if cn.Rd.Buffered() != 0 { if cn.Rd.Buffered() != 0 {
b, _ := cn.Rd.Peek(cn.Rd.Buffered()) b, _ := cn.Rd.Peek(cn.Rd.Buffered())
err := fmt.Errorf("connection has unread data: %q", b) err := fmt.Errorf("connection has unread data: %q", b)
Logger.Print(err) internal.Logf(err.Error())
return p.Remove(cn, err) return p.Remove(cn, err)
} }
p.freeConnsMu.Lock() p.freeConnsMu.Lock()
@ -345,11 +343,11 @@ func (p *ConnPool) reaper(frequency time.Duration) {
} }
n, err := p.ReapStaleConns() n, err := p.ReapStaleConns()
if err != nil { if err != nil {
Logger.Printf("ReapStaleConns failed: %s", err) internal.Logf("ReapStaleConns failed: %s", err)
continue continue
} }
s := p.Stats() s := p.Stats()
Logger.Printf( internal.Logf(
"reaper: removed %d stale conns (TotalConns=%d FreeConns=%d Requests=%d Hits=%d Timeouts=%d)", "reaper: removed %d stale conns (TotalConns=%d FreeConns=%d Requests=%d Hits=%d Timeouts=%d)",
n, s.TotalConns, s.FreeConns, s.Requests, s.Hits, s.Timeouts, n, s.TotalConns, s.FreeConns, s.Requests, s.Hits, s.Timeouts,
) )

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"gopkg.in/redis.v3/internal"
"gopkg.in/redis.v3/internal/pool" "gopkg.in/redis.v3/internal/pool"
) )
@ -59,7 +60,7 @@ func (c *Multi) process(cmd Cmder) {
func (c *Multi) Close() error { func (c *Multi) Close() error {
c.closed = true c.closed = true
if err := c.Unwatch().Err(); err != nil { if err := c.Unwatch().Err(); err != nil {
Logger.Printf("Unwatch failed: %s", err) internal.Logf("Unwatch failed: %s", err)
} }
return c.base.Close() return c.base.Close()
} }

View File

@ -5,6 +5,7 @@ import (
"net" "net"
"time" "time"
"gopkg.in/redis.v3/internal"
"gopkg.in/redis.v3/internal/pool" "gopkg.in/redis.v3/internal/pool"
) )
@ -270,7 +271,7 @@ func (c *PubSub) receiveMessage(timeout time.Duration) (*Message, error) {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() { if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
err := c.Ping("") err := c.Ping("")
if err != nil { if err != nil {
Logger.Printf("PubSub.Ping failed: %s", err) internal.Logf("PubSub.Ping failed: %s", err)
} }
} }
} else { } else {
@ -316,12 +317,12 @@ func (c *PubSub) resubscribe() {
} }
if len(c.channels) > 0 { if len(c.channels) > 0 {
if err := c.Subscribe(c.channels...); err != nil { if err := c.Subscribe(c.channels...); err != nil {
Logger.Printf("Subscribe failed: %s", err) internal.Logf("Subscribe failed: %s", err)
} }
} }
if len(c.patterns) > 0 { if len(c.patterns) > 0 {
if err := c.PSubscribe(c.patterns...); err != nil { if err := c.PSubscribe(c.patterns...); err != nil {
Logger.Printf("PSubscribe failed: %s", err) internal.Logf("PSubscribe failed: %s", err)
} }
} }
} }

View File

@ -2,18 +2,16 @@ package redis // import "gopkg.in/redis.v3"
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"gopkg.in/redis.v3/internal"
"gopkg.in/redis.v3/internal/pool" "gopkg.in/redis.v3/internal/pool"
) )
// Deprecated. Use SetLogger instead. var Logger *log.Logger
var Logger = log.New(ioutil.Discard, "redis: ", log.LstdFlags)
func SetLogger(logger *log.Logger) { func SetLogger(logger *log.Logger) {
Logger = logger internal.Logger = logger
pool.Logger = logger
} }
type baseClient struct { type baseClient struct {

View File

@ -6,6 +6,7 @@ import (
"sync" "sync"
"time" "time"
"gopkg.in/redis.v3/internal"
"gopkg.in/redis.v3/internal/consistenthash" "gopkg.in/redis.v3/internal/consistenthash"
"gopkg.in/redis.v3/internal/hashtag" "gopkg.in/redis.v3/internal/hashtag"
"gopkg.in/redis.v3/internal/pool" "gopkg.in/redis.v3/internal/pool"
@ -204,7 +205,7 @@ func (ring *Ring) heartbeat() {
for _, shard := range ring.shards { for _, shard := range ring.shards {
err := shard.Client.Ping().Err() err := shard.Client.Ping().Err()
if shard.Vote(err == nil || err == pool.ErrPoolTimeout) { if shard.Vote(err == nil || err == pool.ErrPoolTimeout) {
Logger.Printf("ring shard state changed: %s", shard) internal.Logf("ring shard state changed: %s", shard)
rebalance = true rebalance = true
} }
} }

View File

@ -8,6 +8,7 @@ import (
"sync" "sync"
"time" "time"
"gopkg.in/redis.v3/internal"
"gopkg.in/redis.v3/internal/pool" "gopkg.in/redis.v3/internal/pool"
) )
@ -165,11 +166,11 @@ func (d *sentinelFailover) MasterAddr() (string, error) {
if d.sentinel != nil { if d.sentinel != nil {
addr, err := d.sentinel.GetMasterAddrByName(d.masterName).Result() addr, err := d.sentinel.GetMasterAddrByName(d.masterName).Result()
if err != nil { if err != nil {
Logger.Printf("sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) internal.Logf("sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err)
d._resetSentinel() d._resetSentinel()
} else { } else {
addr := net.JoinHostPort(addr[0], addr[1]) addr := net.JoinHostPort(addr[0], addr[1])
Logger.Printf("sentinel: %q addr is %s", d.masterName, addr) internal.Logf("sentinel: %q addr is %s", d.masterName, addr)
return addr, nil return addr, nil
} }
} }
@ -188,7 +189,7 @@ func (d *sentinelFailover) MasterAddr() (string, error) {
}) })
masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result() masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result()
if err != nil { if err != nil {
Logger.Printf("sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) internal.Logf("sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err)
sentinel.Close() sentinel.Close()
continue continue
} }
@ -198,7 +199,7 @@ func (d *sentinelFailover) MasterAddr() (string, error) {
d.setSentinel(sentinel) d.setSentinel(sentinel)
addr := net.JoinHostPort(masterAddr[0], masterAddr[1]) addr := net.JoinHostPort(masterAddr[0], masterAddr[1])
Logger.Printf("sentinel: %q addr is %s", d.masterName, addr) internal.Logf("sentinel: %q addr is %s", d.masterName, addr)
return addr, nil return addr, nil
} }
@ -230,7 +231,7 @@ func (d *sentinelFailover) _resetSentinel() error {
func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) { func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
sentinels, err := sentinel.Sentinels(d.masterName).Result() sentinels, err := sentinel.Sentinels(d.masterName).Result()
if err != nil { if err != nil {
Logger.Printf("sentinel: Sentinels %q failed: %s", d.masterName, err) internal.Logf("sentinel: Sentinels %q failed: %s", d.masterName, err)
return return
} }
for _, sentinel := range sentinels { for _, sentinel := range sentinels {
@ -240,7 +241,7 @@ func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
if key == "name" { if key == "name" {
sentinelAddr := vals[i+1].(string) sentinelAddr := vals[i+1].(string)
if !contains(d.sentinelAddrs, sentinelAddr) { if !contains(d.sentinelAddrs, sentinelAddr) {
Logger.Printf( internal.Logf(
"sentinel: discovered new %q sentinel: %s", "sentinel: discovered new %q sentinel: %s",
d.masterName, sentinelAddr, d.masterName, sentinelAddr,
) )
@ -268,7 +269,7 @@ func (d *sentinelFailover) closeOldConns(newMaster string) {
"sentinel: closing connection to the old master %s", "sentinel: closing connection to the old master %s",
cn.RemoteAddr(), cn.RemoteAddr(),
) )
Logger.Print(err) internal.Logf(err.Error())
d.pool.Remove(cn, err) d.pool.Remove(cn, err)
} else { } else {
cnsToPut = append(cnsToPut, cn) cnsToPut = append(cnsToPut, cn)
@ -286,7 +287,7 @@ func (d *sentinelFailover) listen(sentinel *sentinelClient) {
if pubsub == nil { if pubsub == nil {
pubsub = sentinel.PubSub() pubsub = sentinel.PubSub()
if err := pubsub.Subscribe("+switch-master"); err != nil { if err := pubsub.Subscribe("+switch-master"); err != nil {
Logger.Printf("sentinel: Subscribe failed: %s", err) internal.Logf("sentinel: Subscribe failed: %s", err)
d.resetSentinel() d.resetSentinel()
return return
} }
@ -294,7 +295,7 @@ func (d *sentinelFailover) listen(sentinel *sentinelClient) {
msg, err := pubsub.ReceiveMessage() msg, err := pubsub.ReceiveMessage()
if err != nil { if err != nil {
Logger.Printf("sentinel: ReceiveMessage failed: %s", err) internal.Logf("sentinel: ReceiveMessage failed: %s", err)
pubsub.Close() pubsub.Close()
d.resetSentinel() d.resetSentinel()
return return
@ -304,12 +305,12 @@ func (d *sentinelFailover) listen(sentinel *sentinelClient) {
case "+switch-master": case "+switch-master":
parts := strings.Split(msg.Payload, " ") parts := strings.Split(msg.Payload, " ")
if parts[0] != d.masterName { if parts[0] != d.masterName {
Logger.Printf("sentinel: ignore new %s addr", parts[0]) internal.Logf("sentinel: ignore new %s addr", parts[0])
continue continue
} }
addr := net.JoinHostPort(parts[3], parts[4]) addr := net.JoinHostPort(parts[3], parts[4])
Logger.Printf( internal.Logf(
"sentinel: new %q addr is %s", "sentinel: new %q addr is %s",
d.masterName, addr, d.masterName, addr,
) )