-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontext.go
56 lines (45 loc) · 1.71 KB
/
context.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
52
53
54
55
56
package ssokenizer
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
)
type contextKey string
const (
contextKeyProvider contextKey = "provider"
contextKeyLog contextKey = "log"
)
func withProvider(r *http.Request, p *provider) *http.Request {
return r.WithContext(context.WithValue(r.Context(), contextKeyProvider, p))
}
func getProvider(r *http.Request) *provider {
return r.Context().Value(contextKeyProvider).(*provider)
}
// Updates the logrus.FieldLogger in the context with added data. Requests are
// logged by Transaction.ReturnData/ReturnError.
func WithLog(r *http.Request, l logrus.FieldLogger) *http.Request {
return r.WithContext(context.WithValue(r.Context(), contextKeyLog, l))
}
// Updates the logrus.FieldLogger in the context with "error" field. Requests
// are logged by Transaction.ReturnData/ReturnError.
func WithError(r *http.Request, err error) *http.Request {
return WithLog(r, GetLog(r).WithError(err))
}
// Updates the logrus.FieldLogger in the context with added field. Requests
// are logged by Transaction.ReturnData/ReturnError.
func WithField(r *http.Request, key string, value any) *http.Request {
return WithLog(r, GetLog(r).WithField(key, value))
}
// Updates the logrus.FieldLogger in the context with added fields. Requests
// are logged by Transaction.ReturnData/ReturnError.
func WithFields(r *http.Request, fields logrus.Fields) *http.Request {
return WithLog(r, GetLog(r).WithFields(fields))
}
// Gets the logrus.FieldLogger from the context. Requests are logged by
// Transaction.ReturnData/ReturnError.
func GetLog(r *http.Request) logrus.FieldLogger {
if l, ok := r.Context().Value(contextKeyLog).(logrus.FieldLogger); ok {
return l
}
return logrus.StandardLogger()
}