File size: 5,566 Bytes
a4468f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package logx

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
)

type (
	Logger = logrus.Logger
	Entry  = logrus.Entry
	Hook   = logrus.Hook
	Level  = logrus.Level
)

func Init(ctx context.Context) {
	SetLevel(logrus.DebugLevel)
	SetFormatter(func() logrus.Formatter {
		return &TextFormatter{}
	}())
	WithContext(ctx).Infof("application process in PID: %v", os.Getpid())
}

func SetLevel(level Level) {
	logrus.SetLevel(level)
}

func SetFormatter(format logrus.Formatter) {
	logrus.SetFormatter(format)
}

//
//func SetFormatter(format string) {
//	switch format {
//	case "json":
//		logrus.SetFormatter(&logrus.JSONFormatter{})
//	case "text":
//		logrus.SetFormatter(&TextFormatter{})
//	default:
//		logrus.SetFormatter(&logrus.TextFormatter{})
//	}
//}

func AddHook(hook Hook) {
	logrus.AddHook(hook)
}

const (
	TraceIdKey  = "trace"
	UserIDKey   = "uid"
	GuuIDKey    = "guid"
	SchoolIDKey = "school"
	UsernameKey = "username"
	TagKey      = "tag"
	StackKey    = "stack"
)

type (
	traceIdKey  struct{}
	userIDKey   struct{}
	guuIDKey    struct{}
	usernameKey struct{}
	schoolIdKey struct{}
	tagKey      struct{}
	stackKey    struct{}
	requestKey  struct{}
	responseKey struct{}
	diff1Key    struct{}
	diff2Key    struct{}
	actionKey   struct{}
)

func TraceIdContext(ctx context.Context, traceId string) context.Context {
	return context.WithValue(ctx, traceIdKey{}, traceId)
}

func FromTraceIdContext(ctx context.Context) string {
	if v := ctx.Value(traceIdKey{}); v != nil {
		if s, ok := v.(string); ok {
			return s
		}
	}
	return ""
}

func UserIDContext(ctx context.Context, userId int) context.Context {
	return context.WithValue(ctx, userIDKey{}, userId)
}

func FromUserIDContext(ctx context.Context) int {
	if v := ctx.Value(userIDKey{}); v != nil {
		if s, ok := v.(int); ok {
			return s
		}
	}
	return 0
}

func GuuIDContext(ctx context.Context, userId string) context.Context {
	return context.WithValue(ctx, guuIDKey{}, userId)
}

func FromGuuIDContext(ctx context.Context) string {
	if v := ctx.Value(guuIDKey{}); v != nil {
		if s, ok := v.(string); ok {
			return s
		}
	}
	return ""
}

func SchoolIDContext(ctx context.Context, userId int) context.Context {
	return context.WithValue(ctx, schoolIdKey{}, userId)
}

func FromSchoolIDContext(ctx context.Context) int {
	if v := ctx.Value(schoolIdKey{}); v != nil {
		if s, ok := v.(int); ok {
			return s
		}
	}
	return 0
}

func UsernameContext(ctx context.Context, username string) context.Context {
	return context.WithValue(ctx, usernameKey{}, username)
}

func FromUsernameContext(ctx context.Context) string {
	if v := ctx.Value(usernameKey{}); v != nil {
		if s, ok := v.(string); ok {
			return s
		}
	}
	return ""
}

func TagContext(ctx context.Context, tag string) context.Context {
	return context.WithValue(ctx, tagKey{}, tag)
}

func FromTagContext(ctx context.Context) string {
	if v := ctx.Value(tagKey{}); v != nil {
		if s, ok := v.(string); ok {
			return s
		}
	}
	return ""
}

func ActionContext(ctx context.Context, action string) context.Context {
	return context.WithValue(ctx, actionKey{}, action)
}

func FromActionContext(ctx context.Context) string {
	if v := ctx.Value(actionKey{}); v != nil {
		if s, ok := v.(string); ok {
			return s
		}
	}
	return ""
}

func StackContext(ctx context.Context, stack error) context.Context {
	return context.WithValue(ctx, stackKey{}, stack)
}

func FromStackContext(ctx context.Context) error {
	if v := ctx.Value(tagKey{}); v != nil {
		if e, ok := v.(error); ok {
			return e
		}
	}
	return nil
}

func RequestContext(ctx context.Context, data []byte) context.Context {
	return context.WithValue(ctx, requestKey{}, string(data))
}

func FromRequestContext(ctx context.Context) string {
	if v := ctx.Value(requestKey{}); v != "" {
		if e, ok := v.(string); ok {
			return e
		}
	}
	return ""
}

func ResponseContext(ctx context.Context, data any) context.Context {
	bts, _ := json.Marshal(&data)
	return context.WithValue(ctx, responseKey{}, string(bts))
}

func FromResponseContext(ctx context.Context) string {
	if v := ctx.Value(responseKey{}); v != "" {
		if e, ok := v.(string); ok {
			return e
		}
	}
	return ""
}

func DiffContext(ctx context.Context, data1, data2 any) context.Context {
	ctx = context.WithValue(ctx, diff1Key{}, data1)
	ctx = context.WithValue(ctx, diff2Key{}, data2)
	return ctx
}

func FromDiffContext(ctx context.Context) (any, any) {
	return ctx.Value(diff1Key{}), ctx.Value(diff2Key{})
}

func WithContext(ctx context.Context) *Entry {
	fields := logrus.Fields{}
	if v := FromTraceIdContext(ctx); v != "" {
		fields[TraceIdKey] = v
	}

	if v := FromUserIDContext(ctx); v != 0 {
		fields[UserIDKey] = v
	}

	if v := FromGuuIDContext(ctx); v != "" {
		fields[GuuIDKey] = v
	}

	if v := FromSchoolIDContext(ctx); v != 0 {
		fields[SchoolIDKey] = v
	}

	if v := FromUsernameContext(ctx); v != "" {
		fields[UsernameKey] = v
	}

	if v := FromTagContext(ctx); v != "" {
		fields[TagKey] = v
	}

	if v := FromStackContext(ctx); v != nil {
		fields[StackKey] = fmt.Sprintf("%+v", v)
	}

	return logrus.WithContext(ctx).WithFields(fields)
}

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
	SetOutput       = logrus.SetOutput
	SetReportCaller = logrus.SetReportCaller
	StandardLogger  = logrus.StandardLogger
	ParseLevel      = logrus.ParseLevel
)