From eaf5270b300732d0b62dd4ee50da121b5d4e05f6 Mon Sep 17 00:00:00 2001 From: Qiao Liang Date: Tue, 11 Apr 2017 18:01:36 +0800 Subject: [PATCH] allow basic logging for http Get err with RegisterErrLogFn rename to follow hook function name convention, log more ctx info --- groupcache.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/groupcache.go b/groupcache.go index 316ca494..dbb18168 100644 --- a/groupcache.go +++ b/groupcache.go @@ -26,6 +26,7 @@ package groupcache import ( "errors" + "fmt" "math/rand" "strconv" "sync" @@ -60,6 +61,7 @@ var ( initPeerServerOnce sync.Once initPeerServer func() + errLogHook func(error) ) // GetGroup returns the named group previously created with NewGroup, or @@ -121,6 +123,20 @@ func RegisterNewGroupHook(fn func(*Group)) { newGroupHook = fn } +// RegisterErrLogHook registers a log func that is used for HTTP error logging. +func RegisterErrLogHook(fn func(error)) { + if errLogHook != nil { + panic("RegisterErrLogHook called more than once") + } + errLogHook = fn +} + +func logErr(err error) { + if errLogHook != nil { + errLogHook(err) + } +} + // RegisterServerStart registers a hook that is run when the first // group is created. func RegisterServerStart(fn func()) { @@ -271,10 +287,7 @@ func (g *Group) load(ctx Context, key string, dest Sink) (value ByteView, destPo return value, nil } g.Stats.PeerErrors.Add(1) - // TODO(bradfitz): log the peer's error? keep - // log of the past few for /groupcachez? It's - // probably boring (normal task movement), so not - // worth logging I imagine. + logErr(fmt.Errorf("%+v, %+v, %+v, %+v", ctx, peer, key, err)) } value, err = g.getLocally(ctx, key, dest) if err != nil {