From 093eded07a1787b7fe3a870324b6980ee6214673 Mon Sep 17 00:00:00 2001 From: Yash Sharma Date: Mon, 7 Dec 2020 20:48:52 +0530 Subject: [PATCH] Added an error param for decider method Signed-off-by: Yash Sharma --- interceptors/logging/interceptors.go | 6 +++--- interceptors/logging/interceptors_test.go | 2 +- interceptors/logging/logging.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/interceptors/logging/interceptors.go b/interceptors/logging/interceptors.go index 6d170270b..654223f2a 100644 --- a/interceptors/logging/interceptors.go +++ b/interceptors/logging/interceptors.go @@ -42,7 +42,7 @@ func (c *reporter) logMessage(logger Logger, err error, msg string, duration tim } func (c *reporter) PostCall(err error, duration time.Duration) { - switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method)) { + switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method), err) { case LogFinishCall, LogStartAndFinishCall: if err == io.EOF { err = nil @@ -57,7 +57,7 @@ func (c *reporter) PostMsgSend(_ interface{}, err error, duration time.Duration) if c.startCallLogged { return } - switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method)) { + switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method), err) { case LogStartAndFinishCall: c.startCallLogged = true c.logMessage(c.logger, err, "started call", duration) @@ -68,7 +68,7 @@ func (c *reporter) PostMsgReceive(_ interface{}, err error, duration time.Durati if c.startCallLogged { return } - switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method)) { + switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method), err) { case LogStartAndFinishCall: c.startCallLogged = true c.logMessage(c.logger, err, "started call", duration) diff --git a/interceptors/logging/interceptors_test.go b/interceptors/logging/interceptors_test.go index a4526c143..9cd0a5740 100644 --- a/interceptors/logging/interceptors_test.go +++ b/interceptors/logging/interceptors_test.go @@ -465,7 +465,7 @@ func TestCustomDeciderSuite(t *testing.T) { t.Skip("Skipping due to json.RawMessage incompatibility with go1.7") return } - opts := logging.WithDecider(func(method string) logging.Decision { + opts := logging.WithDecider(func(method string, _ error) logging.Decision { if method == "/grpc_middleware.testpb.TestService/PingError" { return logging.LogStartAndFinishCall } diff --git a/interceptors/logging/logging.go b/interceptors/logging/logging.go index 9f5f2346a..67d6861bd 100644 --- a/interceptors/logging/logging.go +++ b/interceptors/logging/logging.go @@ -60,11 +60,11 @@ func DefaultErrorToCode(err error) codes.Code { } // Decider function defines rules for suppressing any interceptor logs -type Decider func(fullMethodName string) Decision +type Decider func(fullMethodName string, err error) Decision // DefaultDeciderMethod is the default implementation of decider to see if you should log the call // by default this if always true so all calls are logged -func DefaultDeciderMethod(_ string) Decision { +func DefaultDeciderMethod(_ string, _ error) Decision { return LogStartAndFinishCall }