forked from luraproject/lura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
36 lines (30 loc) · 899 Bytes
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// SPDX-License-Identifier: Apache-2.0
package proxy
import (
"context"
"time"
"github.com/davron112/lura/logging"
)
// NewLoggingMiddleware creates proxy middleware for logging requests and responses
func NewLoggingMiddleware(logger logging.Logger, name string) Middleware {
return func(next ...Proxy) Proxy {
if len(next) > 1 {
panic(ErrTooManyProxies)
}
return func(ctx context.Context, request *Request) (*Response, error) {
begin := time.Now()
logger.Info(name, "Calling backend")
logger.Debug("Request", request)
result, err := next[0](ctx, request)
logger.Info(name, "Call to backend took", time.Since(begin).String())
if err != nil {
logger.Warning(name, "Call to backend failed:", err.Error())
return result, err
}
if result == nil {
logger.Warning(name, "Call to backend returned a null response")
}
return result, err
}
}
}