log: report error when ctx key is non-string (#27226)
* log/format.go : invalid string cast fix * log: some polish --------- Co-authored-by: Martin Holst Swende <martin@swende.se>
This commit is contained in:
parent
7ac08ba4e0
commit
81d328a73e
@ -169,7 +169,7 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
|
|||||||
k, ok := ctx[i].(string)
|
k, ok := ctx[i].(string)
|
||||||
v := formatLogfmtValue(ctx[i+1], term)
|
v := formatLogfmtValue(ctx[i+1], term)
|
||||||
if !ok {
|
if !ok {
|
||||||
k, v = errorKey, formatLogfmtValue(k, term)
|
k, v = errorKey, fmt.Sprintf("%+T is not a string key", ctx[i])
|
||||||
} else {
|
} else {
|
||||||
k = escapeString(k)
|
k = escapeString(k)
|
||||||
}
|
}
|
||||||
@ -218,20 +218,20 @@ func JSONFormatOrderedEx(pretty, lineSeparated bool) Format {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return FormatFunc(func(r *Record) []byte {
|
return FormatFunc(func(r *Record) []byte {
|
||||||
props := make(map[string]interface{})
|
props := map[string]interface{}{
|
||||||
|
r.KeyNames.Time: r.Time,
|
||||||
props[r.KeyNames.Time] = r.Time
|
r.KeyNames.Lvl: r.Lvl.String(),
|
||||||
props[r.KeyNames.Lvl] = r.Lvl.String()
|
r.KeyNames.Msg: r.Msg,
|
||||||
props[r.KeyNames.Msg] = r.Msg
|
}
|
||||||
|
|
||||||
ctx := make([]string, len(r.Ctx))
|
ctx := make([]string, len(r.Ctx))
|
||||||
for i := 0; i < len(r.Ctx); i += 2 {
|
for i := 0; i < len(r.Ctx); i += 2 {
|
||||||
k, ok := r.Ctx[i].(string)
|
if k, ok := r.Ctx[i].(string); ok {
|
||||||
if !ok {
|
ctx[i] = k
|
||||||
props[errorKey] = fmt.Sprintf("%+v is not a string key,", r.Ctx[i])
|
ctx[i+1] = formatLogfmtValue(r.Ctx[i+1], true)
|
||||||
|
} else {
|
||||||
|
props[errorKey] = fmt.Sprintf("%+T is not a string key,", r.Ctx[i])
|
||||||
}
|
}
|
||||||
ctx[i] = k
|
|
||||||
ctx[i+1] = formatLogfmtValue(r.Ctx[i+1], true)
|
|
||||||
}
|
}
|
||||||
props[r.KeyNames.Ctx] = ctx
|
props[r.KeyNames.Ctx] = ctx
|
||||||
|
|
||||||
@ -261,18 +261,19 @@ func JSONFormatEx(pretty, lineSeparated bool) Format {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return FormatFunc(func(r *Record) []byte {
|
return FormatFunc(func(r *Record) []byte {
|
||||||
props := make(map[string]interface{})
|
props := map[string]interface{}{
|
||||||
|
r.KeyNames.Time: r.Time,
|
||||||
props[r.KeyNames.Time] = r.Time
|
r.KeyNames.Lvl: r.Lvl.String(),
|
||||||
props[r.KeyNames.Lvl] = r.Lvl.String()
|
r.KeyNames.Msg: r.Msg,
|
||||||
props[r.KeyNames.Msg] = r.Msg
|
}
|
||||||
|
|
||||||
for i := 0; i < len(r.Ctx); i += 2 {
|
for i := 0; i < len(r.Ctx); i += 2 {
|
||||||
k, ok := r.Ctx[i].(string)
|
k, ok := r.Ctx[i].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i])
|
props[errorKey] = fmt.Sprintf("%+T is not a string key", r.Ctx[i])
|
||||||
|
} else {
|
||||||
|
props[k] = formatJSONValue(r.Ctx[i+1])
|
||||||
}
|
}
|
||||||
props[k] = formatJSONValue(r.Ctx[i+1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := jsonMarshal(props)
|
b, err := jsonMarshal(props)
|
||||||
|
Loading…
Reference in New Issue
Block a user