This repository was archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcontext.go
67 lines (54 loc) · 1.78 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
65
66
67
package lg
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
)
var (
LoggerCtxKey = &contextKey{"Logger"}
LogEntryCtxKey = &contextKey{"LogEntry"}
)
func WithLoggerContext(parent context.Context, logger *logrus.Logger) context.Context {
return context.WithValue(parent, LoggerCtxKey, logger)
}
func WithLogEntry(parent context.Context, logEntry *HTTPLoggerEntry) context.Context {
return context.WithValue(parent, LogEntryCtxKey, logEntry)
}
func Log(ctx context.Context) logrus.FieldLogger {
if entry, ok := ctx.Value(LogEntryCtxKey).(*HTTPLoggerEntry); ok {
return entry.Logger
}
lgr, ok := ctx.Value(LoggerCtxKey).(*logrus.Logger)
if !ok {
panic("lg: logger backend has not been set on the context.")
}
return lgr
}
func RequestLog(r *http.Request) logrus.FieldLogger {
return Log(r.Context())
}
func SetEntryField(ctx context.Context, key string, value interface{}) {
if entry, ok := ctx.Value(LogEntryCtxKey).(*HTTPLoggerEntry); ok {
entry.Logger = entry.Logger.WithField(key, value)
}
}
func SetEntryFields(ctx context.Context, fields map[string]interface{}) {
if entry, ok := ctx.Value(LogEntryCtxKey).(*HTTPLoggerEntry); ok {
entry.Logger = entry.Logger.WithFields(fields)
}
}
func SetRequestEntryField(r *http.Request, key string, value interface{}) {
SetEntryField(r.Context(), key, value)
}
func SetRequestEntryFields(r *http.Request, fields map[string]interface{}) {
SetEntryFields(r.Context(), fields)
}
// contextKey is a value for use with context.WithValue. It's used as
// a pointer so it fits in an interface{} without allocation. This technique
// for defining context keys was copied from Go 1.7's new use of context in net/http.
type contextKey struct {
name string
}
func (k *contextKey) String() string {
return "lg context value " + k.name
}