-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
154 lines (124 loc) · 2.63 KB
/
logging.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
package errors
import (
"encoding/json"
"errors"
"time"
)
// FieldLogger used to set error fields into structured logger.
type FieldLogger interface {
SetBool(key string, value bool)
SetInt(key string, value int)
SetUint(key string, value uint)
SetFloat(key string, value float64)
SetString(key string, value string)
SetStrings(key string, values []string)
SetValue(key string, value interface{})
SetTime(key string, value time.Time)
SetDuration(key string, value time.Duration)
SetJSON(key string, value json.RawMessage)
SetStackTrace(trace StackTrace)
}
type Logger interface {
FieldLogger
Log(message string)
}
type Field interface {
Set(logger FieldLogger)
}
type LoggableError interface {
LogFields(logger FieldLogger)
}
func Log(err error, logger Logger) {
if err == nil {
return
}
for e := err; e != nil; e = errors.Unwrap(e) {
if s, ok := e.(stackTracer); ok {
logger.SetStackTrace(s.StackTrace())
}
}
logFields(err, logger)
logger.Log(err.Error())
}
func logFields(err error, logger Logger) {
for e := err; e != nil; e = errors.Unwrap(e) {
if w, ok := e.(LoggableError); ok {
w.LogFields(logger)
}
if joined, ok := e.(interface{ Unwrap() []error }); ok {
for _, u := range joined.Unwrap() {
logFields(u, logger)
}
}
}
}
type BoolField struct {
Key string
Value bool
}
func (f BoolField) Set(logger FieldLogger) {
logger.SetBool(f.Key, f.Value)
}
type IntField struct {
Key string
Value int
}
func (f IntField) Set(logger FieldLogger) {
logger.SetInt(f.Key, f.Value)
}
type UintField struct {
Key string
Value uint
}
func (f UintField) Set(logger FieldLogger) {
logger.SetUint(f.Key, f.Value)
}
type FloatField struct {
Key string
Value float64
}
func (f FloatField) Set(logger FieldLogger) {
logger.SetFloat(f.Key, f.Value)
}
type StringField struct {
Key string
Value string
}
func (f StringField) Set(logger FieldLogger) {
logger.SetString(f.Key, f.Value)
}
type StringsField struct {
Key string
Values []string
}
func (f StringsField) Set(logger FieldLogger) {
logger.SetStrings(f.Key, f.Values)
}
type ValueField struct {
Key string
Value interface{}
}
func (f ValueField) Set(logger FieldLogger) {
logger.SetValue(f.Key, f.Value)
}
type TimeField struct {
Key string
Value time.Time
}
func (f TimeField) Set(logger FieldLogger) {
logger.SetTime(f.Key, f.Value)
}
type DurationField struct {
Key string
Value time.Duration
}
func (f DurationField) Set(logger FieldLogger) {
logger.SetDuration(f.Key, f.Value)
}
type JSONField struct {
Key string
Value json.RawMessage
}
func (f JSONField) Set(logger FieldLogger) {
logger.SetJSON(f.Key, f.Value)
}