Skip to content

Commit 5d059a0

Browse files
gzliudanholiman
andcommitted
log: report error when ctx key is non-string (ethereum#27226)
* log/format.go : invalid string cast fix * log: some polish --------- Co-authored-by: Martin Holst Swende <[email protected]>
1 parent d1519ae commit 5d059a0

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

log/format.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
169169
k, ok := ctx[i].(string)
170170
v := formatLogfmtValue(ctx[i+1], term)
171171
if !ok {
172-
k, v = errorKey, formatLogfmtValue(k, term)
172+
k, v = errorKey, fmt.Sprintf("%+T is not a string key", ctx[i])
173173
} else {
174174
k = escapeString(k)
175175
}
@@ -218,20 +218,20 @@ func JSONFormatOrderedEx(pretty, lineSeparated bool) Format {
218218
}
219219
}
220220
return FormatFunc(func(r *Record) []byte {
221-
props := make(map[string]interface{})
222-
223-
props[r.KeyNames.Time] = r.Time
224-
props[r.KeyNames.Lvl] = r.Lvl.String()
225-
props[r.KeyNames.Msg] = r.Msg
221+
props := map[string]interface{}{
222+
r.KeyNames.Time: r.Time,
223+
r.KeyNames.Lvl: r.Lvl.String(),
224+
r.KeyNames.Msg: r.Msg,
225+
}
226226

227227
ctx := make([]string, len(r.Ctx))
228228
for i := 0; i < len(r.Ctx); i += 2 {
229-
k, ok := r.Ctx[i].(string)
230-
if !ok {
231-
props[errorKey] = fmt.Sprintf("%+v is not a string key,", r.Ctx[i])
229+
if k, ok := r.Ctx[i].(string); ok {
230+
ctx[i] = k
231+
ctx[i+1] = formatLogfmtValue(r.Ctx[i+1], true)
232+
} else {
233+
props[errorKey] = fmt.Sprintf("%+T is not a string key,", r.Ctx[i])
232234
}
233-
ctx[i] = k
234-
ctx[i+1] = formatLogfmtValue(r.Ctx[i+1], true)
235235
}
236236
props[r.KeyNames.Ctx] = ctx
237237

@@ -261,18 +261,19 @@ func JSONFormatEx(pretty, lineSeparated bool) Format {
261261
}
262262

263263
return FormatFunc(func(r *Record) []byte {
264-
props := make(map[string]interface{})
265-
266-
props[r.KeyNames.Time] = r.Time
267-
props[r.KeyNames.Lvl] = r.Lvl.String()
268-
props[r.KeyNames.Msg] = r.Msg
264+
props := map[string]interface{}{
265+
r.KeyNames.Time: r.Time,
266+
r.KeyNames.Lvl: r.Lvl.String(),
267+
r.KeyNames.Msg: r.Msg,
268+
}
269269

270270
for i := 0; i < len(r.Ctx); i += 2 {
271271
k, ok := r.Ctx[i].(string)
272272
if !ok {
273-
props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i])
273+
props[errorKey] = fmt.Sprintf("%+T is not a string key", r.Ctx[i])
274+
} else {
275+
props[k] = formatJSONValue(r.Ctx[i+1])
274276
}
275-
props[k] = formatJSONValue(r.Ctx[i+1])
276277
}
277278

278279
b, err := jsonMarshal(props)

0 commit comments

Comments
 (0)