Skip to content

Commit be597e7

Browse files
lvan100lianghuan
authored andcommitted
fix(log): fix error handling in field encoder
1 parent 66615d0 commit be597e7

File tree

5 files changed

+98
-682
lines changed

5 files changed

+98
-682
lines changed

log/field.go

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ import (
2222

2323
const MsgKey = "msg"
2424

25-
// Field represents a log entry field with a key and a corresponding value.
25+
// Field represents a structured log field with a key and a value.
2626
type Field struct {
27-
Key string // The name of the log field.
28-
Val Value // The value of the log field.
27+
Key string // The name of the field.
28+
Val Value // The value of the field.
2929
}
3030

31-
// Msg creates a Field with the key "msg" and a string message value.
31+
// Msg creates a string Field with the "msg" key.
3232
func Msg(msg string) Field {
3333
return String(MsgKey, msg)
3434
}
3535

36-
// Msgf formats the message using fmt.Sprintf and returns a Field with key "msg".
36+
// Msgf formats a message using fmt.Sprintf and creates a string Field with "msg" key.
3737
func Msgf(format string, args ...any) Field {
3838
return String(MsgKey, fmt.Sprintf(format, args...))
3939
}
@@ -43,186 +43,186 @@ func Reflect(key string, val interface{}) Field {
4343
return Field{Key: key, Val: ReflectValue{Val: val}}
4444
}
4545

46-
// Nil creates a Field for a given key with a nil value.
46+
// Nil creates a Field with a nil value.
4747
func Nil(key string) Field {
4848
return Reflect(key, nil)
4949
}
5050

51-
// Bool creates a boolean Field.
51+
// Bool creates a Field for a boolean value.
5252
func Bool(key string, val bool) Field {
5353
return Field{Key: key, Val: BoolValue(val)}
5454
}
5555

56-
// BoolPtr creates a boolean Field from a pointer; returns Nil if pointer is nil.
56+
// BoolPtr creates a Field from a *bool, or a nil Field if the pointer is nil.
5757
func BoolPtr(key string, val *bool) Field {
5858
if val == nil {
5959
return Nil(key)
6060
}
6161
return Bool(key, *val)
6262
}
6363

64-
// Int creates an integer Field.
64+
// Int creates a Field for an int value.
6565
func Int(key string, val int) Field {
6666
return Field{Key: key, Val: Int64Value(val)}
6767
}
6868

69-
// IntPtr creates an integer Field from a pointer; returns Nil if pointer is nil.
69+
// IntPtr creates a Field from a *int, or returns Nil if pointer is nil.
7070
func IntPtr(key string, val *int) Field {
7171
if val == nil {
7272
return Nil(key)
7373
}
7474
return Int(key, *val)
7575
}
7676

77-
// Int8 creates an int8 Field.
77+
// Int8 creates a Field for an int8 value.
7878
func Int8(key string, val int8) Field {
7979
return Field{Key: key, Val: Int64Value(val)}
8080
}
8181

82-
// Int8Ptr creates an int8 Field from a pointer; returns Nil if pointer is nil.
82+
// Int8Ptr creates a Field from a *int8, or returns Nil if pointer is nil.
8383
func Int8Ptr(key string, val *int8) Field {
8484
if val == nil {
8585
return Nil(key)
8686
}
8787
return Int8(key, *val)
8888
}
8989

90-
// Int16 creates an int16 Field.
90+
// Int16 creates a Field for an int16 value.
9191
func Int16(key string, val int16) Field {
9292
return Field{Key: key, Val: Int64Value(val)}
9393
}
9494

95-
// Int16Ptr creates an int16 Field from a pointer; returns Nil if pointer is nil.
95+
// Int16Ptr creates a Field from a *int16, or returns Nil if pointer is nil.
9696
func Int16Ptr(key string, val *int16) Field {
9797
if val == nil {
9898
return Nil(key)
9999
}
100100
return Int16(key, *val)
101101
}
102102

103-
// Int32 creates an int32 Field.
103+
// Int32 creates a Field for an int32 value.
104104
func Int32(key string, val int32) Field {
105105
return Field{Key: key, Val: Int64Value(val)}
106106
}
107107

108-
// Int32Ptr creates an int32 Field from a pointer; returns Nil if pointer is nil.
108+
// Int32Ptr creates a Field from a *int32, or returns Nil if pointer is nil.
109109
func Int32Ptr(key string, val *int32) Field {
110110
if val == nil {
111111
return Nil(key)
112112
}
113113
return Int32(key, *val)
114114
}
115115

116-
// Int64 creates an int64 Field.
116+
// Int64 creates a Field for an int64 value.
117117
func Int64(key string, val int64) Field {
118118
return Field{Key: key, Val: Int64Value(val)}
119119
}
120120

121-
// Int64Ptr creates an int64 Field from a pointer; returns Nil if pointer is nil.
121+
// Int64Ptr creates a Field from a *int64, or returns Nil if pointer is nil.
122122
func Int64Ptr(key string, val *int64) Field {
123123
if val == nil {
124124
return Nil(key)
125125
}
126126
return Int64(key, *val)
127127
}
128128

129-
// Uint creates a uint Field.
129+
// Uint creates a Field for an uint value.
130130
func Uint(key string, val uint) Field {
131131
return Field{Key: key, Val: Uint64Value(val)}
132132
}
133133

134-
// UintPtr creates an uint Field from a pointer; returns Nil if pointer is nil.
134+
// UintPtr creates a Field from a *uint, or returns Nil if pointer is nil.
135135
func UintPtr(key string, val *uint) Field {
136136
if val == nil {
137137
return Nil(key)
138138
}
139139
return Uint(key, *val)
140140
}
141141

142-
// Uint8 creates a uint8 Field.
142+
// Uint8 creates a Field for an uint8 value.
143143
func Uint8(key string, val uint8) Field {
144144
return Field{Key: key, Val: Uint64Value(val)}
145145
}
146146

147-
// Uint8Ptr creates an uint8 Field from a pointer; returns Nil if pointer is nil.
147+
// Uint8Ptr creates a Field from a *uint8, or returns Nil if pointer is nil.
148148
func Uint8Ptr(key string, val *uint8) Field {
149149
if val == nil {
150150
return Nil(key)
151151
}
152152
return Uint8(key, *val)
153153
}
154154

155-
// Uint16 creates a uint16 Field.
155+
// Uint16 creates a Field for an uint16 value.
156156
func Uint16(key string, val uint16) Field {
157157
return Field{Key: key, Val: Uint64Value(val)}
158158
}
159159

160-
// Uint16Ptr creates an uint16 Field from a pointer; returns Nil if pointer is nil.
160+
// Uint16Ptr creates a Field from a *uint16, or returns Nil if pointer is nil.
161161
func Uint16Ptr(key string, val *uint16) Field {
162162
if val == nil {
163163
return Nil(key)
164164
}
165165
return Uint16(key, *val)
166166
}
167167

168-
// Uint32 creates a uint32 Field.
168+
// Uint32 creates a Field for an uint32 value.
169169
func Uint32(key string, val uint32) Field {
170170
return Field{Key: key, Val: Uint64Value(val)}
171171
}
172172

173-
// Uint32Ptr creates an uint32 Field from a pointer; returns Nil if pointer is nil.
173+
// Uint32Ptr creates a Field from a *uint32, or returns Nil if pointer is nil.
174174
func Uint32Ptr(key string, val *uint32) Field {
175175
if val == nil {
176176
return Nil(key)
177177
}
178178
return Uint32(key, *val)
179179
}
180180

181-
// Uint64 creates a uint64 Field.
181+
// Uint64 creates a Field for an uint64 value.
182182
func Uint64(key string, val uint64) Field {
183183
return Field{Key: key, Val: Uint64Value(val)}
184184
}
185185

186-
// Uint64Ptr creates an uint64 Field from a pointer; returns Nil if pointer is nil.
186+
// Uint64Ptr creates a Field from a *uint64, or returns Nil if pointer is nil.
187187
func Uint64Ptr(key string, val *uint64) Field {
188188
if val == nil {
189189
return Nil(key)
190190
}
191191
return Uint64(key, *val)
192192
}
193193

194-
// Float32 creates a float32 Field.
194+
// Float32 creates a Field for a float32 value.
195195
func Float32(key string, val float32) Field {
196196
return Field{Key: key, Val: Float64Value(val)}
197197
}
198198

199-
// Float32Ptr creates a float32 Field from a pointer; returns Nil if pointer is nil.
199+
// Float32Ptr creates a Field from a *float32, or returns Nil if pointer is nil.
200200
func Float32Ptr(key string, val *float32) Field {
201201
if val == nil {
202202
return Nil(key)
203203
}
204204
return Float32(key, *val)
205205
}
206206

207-
// Float64 creates a float64 Field.
207+
// Float64 creates a Field for a float64 value.
208208
func Float64(key string, val float64) Field {
209209
return Field{Key: key, Val: Float64Value(val)}
210210
}
211211

212-
// Float64Ptr creates a float64 Field from a pointer; returns Nil if pointer is nil.
212+
// Float64Ptr creates a Field from a *float64, or returns Nil if pointer is nil.
213213
func Float64Ptr(key string, val *float64) Field {
214214
if val == nil {
215215
return Nil(key)
216216
}
217217
return Float64(key, *val)
218218
}
219219

220-
// String creates a string Field.
220+
// String creates a Field for a string value.
221221
func String(key string, val string) Field {
222222
return Field{Key: key, Val: StringValue(val)}
223223
}
224224

225-
// StringPtr creates a string Field from a pointer; returns Nil if pointer is nil.
225+
// StringPtr creates a Field from a *string, or returns Nil if pointer is nil.
226226
func StringPtr(key string, val *string) Field {
227227
if val == nil {
228228
return Nil(key)
@@ -240,72 +240,72 @@ func Object(key string, fields ...Field) Field {
240240
return Field{Key: key, Val: ObjectValue(fields)}
241241
}
242242

243-
// Bools creates a Field containing a slice of boolean values.
243+
// Bools creates a Field with a slice of booleans.
244244
func Bools(key string, val []bool) Field {
245245
return Field{Key: key, Val: BoolsValue(val)}
246246
}
247247

248-
// Ints creates a Field containing a slice of int values.
248+
// Ints creates a Field with a slice of integers.
249249
func Ints(key string, val []int) Field {
250250
return Field{Key: key, Val: IntsValue(val)}
251251
}
252252

253-
// Int8s creates a Field containing a slice of int8 values.
253+
// Int8s creates a Field with a slice of int8 values.
254254
func Int8s(key string, val []int8) Field {
255255
return Field{Key: key, Val: Int8sValue(val)}
256256
}
257257

258-
// Int16s creates a Field containing a slice of int16 values.
258+
// Int16s creates a Field with a slice of int16 values.
259259
func Int16s(key string, val []int16) Field {
260260
return Field{Key: key, Val: Int16sValue(val)}
261261
}
262262

263-
// Int32s creates a Field containing a slice of int32 values.
263+
// Int32s creates a Field with a slice of int32 values.
264264
func Int32s(key string, val []int32) Field {
265265
return Field{Key: key, Val: Int32sValue(val)}
266266
}
267267

268-
// Int64s creates a Field containing a slice of int64 values.
268+
// Int64s creates a Field with a slice of int64 values.
269269
func Int64s(key string, val []int64) Field {
270270
return Field{Key: key, Val: Int64sValue(val)}
271271
}
272272

273-
// Uints creates a Field containing a slice of uint values.
273+
// Uints creates a Field with a slice of unsigned integers.
274274
func Uints(key string, val []uint) Field {
275275
return Field{Key: key, Val: UintsValue(val)}
276276
}
277277

278-
// Uint8s creates a Field containing a slice of uint8 values.
278+
// Uint8s creates a Field with a slice of uint8 values.
279279
func Uint8s(key string, val []uint8) Field {
280280
return Field{Key: key, Val: Uint8sValue(val)}
281281
}
282282

283-
// Uint16s creates a Field containing a slice of uint16 values.
283+
// Uint16s creates a Field with a slice of uint16 values.
284284
func Uint16s(key string, val []uint16) Field {
285285
return Field{Key: key, Val: Uint16sValue(val)}
286286
}
287287

288-
// Uint32s creates a Field containing a slice of uint32 values.
288+
// Uint32s creates a Field with a slice of uint32 values.
289289
func Uint32s(key string, val []uint32) Field {
290290
return Field{Key: key, Val: Uint32sValue(val)}
291291
}
292292

293-
// Uint64s creates a Field containing a slice of uint64 values.
293+
// Uint64s creates a Field with a slice of uint64 values.
294294
func Uint64s(key string, val []uint64) Field {
295295
return Field{Key: key, Val: Uint64sValue(val)}
296296
}
297297

298-
// Float32s creates a Field containing a slice of float32 values.
298+
// Float32s creates a Field with a slice of float32 values.
299299
func Float32s(key string, val []float32) Field {
300300
return Field{Key: key, Val: Float32sValue(val)}
301301
}
302302

303-
// Float64s creates a Field containing a slice of float64 values.
303+
// Float64s creates a Field with a slice of float64 values.
304304
func Float64s(key string, val []float64) Field {
305305
return Field{Key: key, Val: Float64sValue(val)}
306306
}
307307

308-
// Strings creates a Field containing a slice of string values.
308+
// Strings creates a Field with a slice of strings.
309309
func Strings(key string, val []string) Field {
310310
return Field{Key: key, Val: StringsValue(val)}
311311
}

0 commit comments

Comments
 (0)