-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
60 lines (51 loc) · 1.27 KB
/
log.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
package xqueue
import (
"context"
"fmt"
"github.com/liuxp0827/log"
)
type messageIDKey struct{}
type redeliveryCountKey struct{}
type topicKey struct{}
// MessageID returns a MessageID valuer.
func MessageID() log.Valuer {
return func(ctx context.Context) interface{} {
if ctx != nil {
if msgID, ok := ctx.Value(messageIDKey{}).(string); ok {
return msgID
}
}
return ""
}
}
func MessageIDContext(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, messageIDKey{}, id)
}
// RedeliveryCount returns a RedeliveryCount valuer.
func RedeliveryCount() log.Valuer {
return func(ctx context.Context) interface{} {
if ctx != nil {
if count, ok := ctx.Value(redeliveryCountKey{}).(int); ok {
return fmt.Sprintf("%d", count)
}
}
return ""
}
}
func RedeliveryCountContext(ctx context.Context, count int) context.Context {
return context.WithValue(ctx, redeliveryCountKey{}, count)
}
// Topic returns a Topic valuer.
func Topic() log.Valuer {
return func(ctx context.Context) interface{} {
if ctx != nil {
if topic, ok := ctx.Value(topicKey{}).(string); ok {
return topic
}
}
return ""
}
}
func TopicContext(ctx context.Context, topic string) context.Context {
return context.WithValue(ctx, topicKey{}, topic)
}