forked from rafecolton/negroni-logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
64 lines (50 loc) · 1.5 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
57
58
59
60
61
62
63
64
package negronilogrus
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
)
type ctxLoggerMarker struct{}
type ctxWriterKey struct{}
type ctxLogger struct {
logger *logrus.Entry
fields logrus.Fields
}
var (
ctxLoggerKey = &ctxLoggerMarker{}
)
// Extract takes the call-scoped logrus.Entry from ctx_logrus middleware.
//
// If the ctx_logrus middleware wasn't used, a no-op `logrus.Entry` is returned. This makes it safe to
// use regardless.
func Extract(ctx context.Context) *logrus.Entry {
l, ok := ctx.Value(ctxLoggerKey).(*ctxLogger)
if !ok || l == nil {
return logrus.NewEntry(nullLogger)
}
fields := logrus.Fields{}
// Add logrus fields added until now.
for k, v := range l.fields {
fields[k] = v
}
return l.logger.WithFields(fields)
}
// ToContext adds the logrus.Entry to the context for extraction later.
// Returning the new context that has been created.
func ToContext(ctx context.Context, entry *logrus.Entry) context.Context {
l := &ctxLogger{
logger: entry,
fields: logrus.Fields{},
}
return context.WithValue(ctx, ctxLoggerKey, l)
}
//ExtractWriter extracts ResponseWriter
func ExtractWriter(ctx context.Context) http.ResponseWriter {
rw, _ := ctx.Value(ctxWriterKey{}).(http.ResponseWriter)
return rw
}
// AddWriterToContext adds ResponseWriter to the context for extraction later.
// Returning the new context that has been created.
func AddWriterToContext(ctx context.Context, rw http.ResponseWriter) context.Context {
return context.WithValue(ctx, ctxWriterKey{}, rw)
}