diff options
Diffstat (limited to 'logging')
-rw-r--r-- | logging/middleware.go | 47 |
1 files changed, 20 insertions, 27 deletions
diff --git a/logging/middleware.go b/logging/middleware.go index cbb345a..01278e1 100644 --- a/logging/middleware.go +++ b/logging/middleware.go @@ -3,6 +3,7 @@ package logging import ( "context" "errors" + "fmt" "io" "time" @@ -12,18 +13,9 @@ import ( func LogRequests(logger Logger) gus.Middleware { return func(inner gus.Handler) gus.Handler { return func(ctx context.Context, request *gus.Request) *gus.Response { - start := time.Now() - response := inner(ctx, request) if response != nil { - body := loggedResponseBody{ - request: request, - response: response, - body: response.Body, - start: start, - logger: logger, - } - response.Body = &body + response.Body = loggingBody(logger, request, response) } return response @@ -42,23 +34,6 @@ type loggedResponseBody struct { logger Logger } -func loggingBody(logger Logger, request *gus.Request, response *gus.Response) io.Reader { - body := &loggedResponseBody{ - request: request, - response: response, - body: response.Body, - start: time.Now(), - written: 0, - logger: logger, - } - - if _, ok := response.Body.(io.WriterTo); ok { - return loggedWriteToResponseBody{body} - } - - return body -} - func (lr *loggedResponseBody) log() { end := time.Now() _ = lr.logger.Log( @@ -98,6 +73,7 @@ type loggedWriteToResponseBody struct { } func (lwtr loggedWriteToResponseBody) WriteTo(dst io.Writer) (int64, error) { + fmt.Println("lwtrb.WriteTo()") n, err := lwtr.body.(io.WriterTo).WriteTo(dst) if err == nil { lwtr.written += int(n) @@ -105,3 +81,20 @@ func (lwtr loggedWriteToResponseBody) WriteTo(dst io.Writer) (int64, error) { } return n, err } + +func loggingBody(logger Logger, request *gus.Request, response *gus.Response) io.Reader { + body := &loggedResponseBody{ + request: request, + response: response, + body: response.Body, + start: time.Now(), + written: 0, + logger: logger, + } + + if _, ok := response.Body.(io.WriterTo); ok { + return loggedWriteToResponseBody{body} + } + + return body +} |