forked from vmkteam/zenrpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
35 lines (30 loc) · 914 Bytes
/
logger.go
File metadata and controls
35 lines (30 loc) · 914 Bytes
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
package middleware
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/vmkteam/zenrpc/v2"
)
// WithAPILogger logs via Printf function (e.g. log.Printf) all requests.
func WithAPILogger(pf Printf, serverName string) zenrpc.MiddlewareFunc {
return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc {
return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response {
start := time.Now()
r := h(ctx, w, method, params)
methodName := fullMethodName(serverName, zenrpc.NamespaceFromContext(ctx), method)
pf("ip=%s platform=%q version=%q method=%s duration=%v params=%q err=%q userAgent=%q xRequestId=%q",
IPFromContext(ctx),
PlatformFromContext(ctx),
VersionFromContext(ctx),
methodName,
time.Since(start),
params,
r.Error,
UserAgentFromContext(ctx),
XRequestIDFromContext(ctx),
)
return r
}
}
}