/
logging.go
51 lines (44 loc) · 1.1 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
)
const (
requestIDKey = iota
pathKey
)
func beforeIDExtractor(ctx context.Context, r *http.Request) context.Context {
return context.WithValue(ctx, requestIDKey, r.Header.Get("X-Request-Id"))
}
func beforePATHExtractor(ctx context.Context, r *http.Request) context.Context {
return context.WithValue(ctx, pathKey, r.URL.EscapedPath())
}
func loggingMiddlware(l log.Logger) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (result interface{}, err error) {
var req, resp string
defer func(b time.Time) {
l.Log(
"path", ctx.Value(pathKey),
"request", req,
"result", resp,
"err", err,
"request_id", ctx.Value(requestIDKey),
"elapsed", time.Since(b),
)
}(time.Now())
if r, ok := request.(fmt.Stringer); ok {
req = r.String()
}
result, err = next(ctx, request)
if r, ok := result.(fmt.Stringer); ok {
resp = r.String()
}
return
}
}
}