-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
185 lines (144 loc) · 4.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
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
package logging
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
)
const (
ScopeField = "scope"
ProjectField = "project"
ShopField = "shop"
CheckoutField = "checkout"
OrderField = "order"
TransactionField = "transaction"
CheckoutDeviceField = "checkoutDevice"
DurationField = "duration"
FlakyField = "flaky"
TypeField = "type"
TypeDeprecation = "deprecation"
TypeCall = "call"
TypeAccess = "access"
TypeApplication = "application"
TypeLifecycle = "lifecycle"
TypeCacheinfo = "cacheinfo"
)
type Identifiable interface {
LogIdentity() map[string]any
}
var Log *Logger
type Logger struct {
*logrus.Logger
config *LogConfig
}
func (logger *Logger) WithError(err error) *Entry {
return NewEntry(logger).WithError(err)
}
func (logger *Logger) WithContext(ctx context.Context) *Entry {
return NewEntry(logger).WithContext(ctx)
}
func (logger *Logger) WithField(key string, value interface{}) *Entry {
return NewEntry(logger).WithField(key, value)
}
func (logger *Logger) WithFields(fields logrus.Fields) *Entry {
return NewEntry(logger).WithFields(fields)
}
func (logger *Logger) With(os ...Identifiable) *Entry {
return NewEntry(logger).With(os...)
}
func (logger *Logger) WithTime(t time.Time) *Entry {
return NewEntry(logger).WithTime(t)
}
func (logger *Logger) WithDuration(d time.Duration) *Entry {
return NewEntry(logger).WithDuration(d)
}
func (logger *Logger) WithScope(scope string) *Entry {
return NewEntry(logger).WithScope(scope)
}
func (logger *Logger) WithProject(projectID string) *Entry {
return NewEntry(logger).WithField(ProjectField, projectID)
}
func (logger *Logger) WithShop(shopID string) *Entry {
return NewEntry(logger).WithField(ShopField, shopID)
}
func (logger *Logger) WithCheckout(checkoutID string) *Entry {
return NewEntry(logger).WithField(CheckoutField, checkoutID)
}
func (logger *Logger) WithCheckoutDevice(checkoutDeviceID string) *Entry {
return NewEntry(logger).WithField(CheckoutDeviceField, checkoutDeviceID)
}
func (logger *Logger) WithOrder(order string) *Entry {
return NewEntry(logger).WithField(OrderField, order)
}
func (logger *Logger) WithTransaction(txn string) *Entry {
return NewEntry(logger).WithField(TransactionField, txn)
}
type Entry struct {
*logrus.Entry
}
func NewEntry(logger *Logger) *Entry {
return wrapEntry(logrus.NewEntry(logger.Logger))
}
func (entry *Entry) WithError(err error) *Entry {
withError := entry.WithField(logrus.ErrorKey, err)
stacktrace := ExtractStacktrace(err)
if stacktrace != nil {
return withError.WithField("stacktrace", fmt.Sprintf("%+v", stacktrace))
}
return withError
}
func (entry *Entry) WithContext(ctx context.Context) *Entry {
return wrapEntry(entry.Entry.WithContext(ctx))
}
func (entry *Entry) WithField(key string, value interface{}) *Entry {
return entry.WithFields(logrus.Fields{key: value})
}
func (entry *Entry) WithFields(fields logrus.Fields) *Entry {
return wrapEntry(entry.Entry.WithFields(fields))
}
func (entry *Entry) With(os ...Identifiable) *Entry {
merged := map[string]any{}
for _, o := range os {
fields := o.LogIdentity()
for k, v := range fields {
merged[k] = v
}
}
return wrapEntry(entry.Entry.WithFields(merged))
}
func (entry *Entry) WithTime(t time.Time) *Entry {
return wrapEntry(entry.Entry.WithTime(t))
}
func (entry *Entry) WithDuration(d time.Duration) *Entry {
return entry.WithField(DurationField, d.Nanoseconds()/1000000)
}
func (entry *Entry) WithScope(scope string) *Entry {
return entry.WithField(ScopeField, scope)
}
func (entry *Entry) WithProject(projectID string) *Entry {
return entry.WithField(ProjectField, projectID)
}
func (entry *Entry) WithShop(shop string) *Entry {
return entry.WithField(ShopField, shop)
}
func (entry *Entry) WithCheckout(checkoutID string) *Entry {
return entry.WithField(CheckoutField, checkoutID)
}
func (entry *Entry) WithCheckoutDevice(checkoutDeviceID string) *Entry {
return entry.WithField(CheckoutDeviceField, checkoutDeviceID)
}
func (entry *Entry) WithOrder(orderID string) *Entry {
return entry.WithField(OrderField, orderID)
}
func (entry *Entry) WithTransaction(txnID string) *Entry {
return entry.WithField(TransactionField, txnID)
}
// Deprecation marks the log entry with type "deprecation". This is used for log entries, that are logged during a
// feature change. Logs with "deprecation" types should not be considered as critical and should only occur temporarily
// during the transition.
func (entry *Entry) Deprecation() *Entry {
return entry.WithField(TypeField, TypeDeprecation)
}
func wrapEntry(logrusEntry *logrus.Entry) *Entry {
return &Entry{Entry: logrusEntry}
}