From 3bac9a19a4c957026ae8621ca39d6cfef48d8034 Mon Sep 17 00:00:00 2001 From: ucwong Date: Mon, 8 May 2023 14:06:38 +0800 Subject: [PATCH 1/2] log/format.go : invalid string cast fix --- log/format.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/log/format.go b/log/format.go index b10786efa04..5ee47015c99 100644 --- a/log/format.go +++ b/log/format.go @@ -218,20 +218,20 @@ func JSONFormatOrderedEx(pretty, lineSeparated bool) Format { } } return FormatFunc(func(r *Record) []byte { - props := make(map[string]interface{}) - - props[r.KeyNames.Time] = r.Time - props[r.KeyNames.Lvl] = r.Lvl.String() - props[r.KeyNames.Msg] = r.Msg + props := map[string]interface{}{ + r.KeyNames.Time: r.Time, + r.KeyNames.Lvl: r.Lvl.String(), + r.KeyNames.Msg: r.Msg, + } ctx := make([]string, len(r.Ctx)) for i := 0; i < len(r.Ctx); i += 2 { - k, ok := r.Ctx[i].(string) - if !ok { + if k, ok := r.Ctx[i].(string); ok { + ctx[i] = k + ctx[i+1] = formatLogfmtValue(r.Ctx[i+1], true) + } else { props[errorKey] = fmt.Sprintf("%+v 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 @@ -261,11 +261,11 @@ func JSONFormatEx(pretty, lineSeparated bool) Format { } return FormatFunc(func(r *Record) []byte { - props := make(map[string]interface{}) - - props[r.KeyNames.Time] = r.Time - props[r.KeyNames.Lvl] = r.Lvl.String() - props[r.KeyNames.Msg] = r.Msg + props := map[string]interface{}{ + r.KeyNames.Time: r.Time, + r.KeyNames.Lvl: r.Lvl.String(), + r.KeyNames.Msg: r.Msg, + } for i := 0; i < len(r.Ctx); i += 2 { k, ok := r.Ctx[i].(string) From 7a0bad728feeed27927b5fab2f765f2dfc2f324e Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 8 May 2023 09:10:36 +0200 Subject: [PATCH 2/2] log: some polish --- log/format.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/log/format.go b/log/format.go index 5ee47015c99..fb476b73e32 100644 --- a/log/format.go +++ b/log/format.go @@ -169,7 +169,7 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) { k, ok := ctx[i].(string) v := formatLogfmtValue(ctx[i+1], term) if !ok { - k, v = errorKey, formatLogfmtValue(k, term) + k, v = errorKey, fmt.Sprintf("%+T is not a string key", ctx[i]) } else { k = escapeString(k) } @@ -230,7 +230,7 @@ func JSONFormatOrderedEx(pretty, lineSeparated bool) Format { ctx[i] = k ctx[i+1] = formatLogfmtValue(r.Ctx[i+1], true) } else { - 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]) } } props[r.KeyNames.Ctx] = ctx @@ -270,9 +270,10 @@ func JSONFormatEx(pretty, lineSeparated bool) Format { for i := 0; i < len(r.Ctx); i += 2 { k, ok := r.Ctx[i].(string) 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)