-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger.go
150 lines (125 loc) · 2.65 KB
/
logger.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package logger
import (
"context"
"fmt"
"io"
"github.com/sirupsen/logrus"
)
// Define key
const (
TraceIDKey = "trace_id"
UserIDKey = "user_id"
TagKey = "tag"
VersionKey = "version"
StackKey = "stack"
)
var version string
type Logger = logrus.Logger
type Entry = logrus.Entry
type Hook = logrus.Hook
func StandardLogger() *Logger {
return logrus.StandardLogger()
}
func SetLevel(level int) {
logrus.SetLevel(logrus.Level(level))
}
func SetFormatter(format string) {
switch format {
case "json":
logrus.SetFormatter(new(logrus.JSONFormatter))
default:
logrus.SetFormatter(new(logrus.TextFormatter))
}
}
func SetOutput(out io.Writer) {
logrus.SetOutput(out)
}
func SetVersion(v string) {
version = v
}
func AddHook(hook Hook) {
logrus.AddHook(hook)
}
type (
traceIDKey struct{}
userIDKey struct{}
tagKey struct{}
stackKey struct{}
)
func NewTraceIDContext(ctx context.Context, traceID string) context.Context {
return context.WithValue(ctx, traceIDKey{}, traceID)
}
func FromTraceIDContext(ctx context.Context) string {
v := ctx.Value(traceIDKey{})
if v != nil {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
func NewUserIDContext(ctx context.Context, userID string) context.Context {
return context.WithValue(ctx, userIDKey{}, userID)
}
func FromUserIDContext(ctx context.Context) string {
v := ctx.Value(userIDKey{})
if v != nil {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
func NewTagContext(ctx context.Context, tag string) context.Context {
return context.WithValue(ctx, tagKey{}, tag)
}
func FromTagContext(ctx context.Context) string {
v := ctx.Value(tagKey{})
if v != nil {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
func NewStackContext(ctx context.Context, stack error) context.Context {
return context.WithValue(ctx, stackKey{}, stack)
}
func FromStackContext(ctx context.Context) error {
v := ctx.Value(stackKey{})
if v != nil {
if s, ok := v.(error); ok {
return s
}
}
return nil
}
func WithErrorStack(ctx context.Context, err error) *Entry {
if ctx == nil {
ctx = context.Background()
}
return WithContext(NewStackContext(ctx, err))
}
func WithContext(ctx context.Context) *Entry {
if ctx == nil {
ctx = context.Background()
}
fields := map[string]interface{}{
VersionKey: version,
}
if v := FromStackContext(ctx); v != nil {
fields[StackKey] = fmt.Sprintf("%+v", v)
}
return logrus.WithContext(ctx).WithFields(fields)
}
// Define logrus alias
var (
Tracef = logrus.Tracef
Debugf = logrus.Debugf
Infof = logrus.Infof
Warnf = logrus.Warnf
Errorf = logrus.Errorf
Fatalf = logrus.Fatalf
Panicf = logrus.Panicf
Printf = logrus.Printf
)