From fdc6d5e60137393c4c507c35556647f3dafc917a Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 1 May 2023 10:25:22 -0600 Subject: include the time spent in the handler for the 'dur' log key --- logging/middleware.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/logging/middleware.go b/logging/middleware.go index 3b68c15..ffce1b7 100644 --- a/logging/middleware.go +++ b/logging/middleware.go @@ -12,9 +12,10 @@ import ( func LogRequests(logger Logger) sr.Middleware { return func(inner sr.Handler) sr.Handler { return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { + start := time.Now() response := inner.Handle(ctx, request) if response != nil { - response.Body = loggingBody(logger, request, response) + response.Body = loggingBody(logger, request, response, start) } return response @@ -81,12 +82,12 @@ func (lwtr loggedWriteToResponseBody) WriteTo(dst io.Writer) (int64, error) { return n, err } -func loggingBody(logger Logger, request *sr.Request, response *sr.Response) io.Reader { +func loggingBody(logger Logger, request *sr.Request, response *sr.Response, start time.Time) io.Reader { body := &loggedResponseBody{ request: request, response: response, body: response.Body, - start: time.Now(), + start: start, written: 0, logger: logger, } -- cgit v1.2.3