add a +1 with field growth to try minimise allocation for log 'msg' field

This commit is contained in:
kim 2024-11-11 15:14:36 +00:00
parent c4461809ea
commit 26b6fe9b05

View file

@ -413,8 +413,8 @@ func logf(ctx context.Context, depth int, lvl LEVEL, fields []kv.Field, s string
buf.B = append(buf.B, ' ')
if ctx != nil && len(ctxhooks) > 0 {
// Ensure fields have extra necessary space.
fields = xslices.GrowJust(fields, len(ctxhooks))
// Ensure fields have space for hooks (+1 for below).
fields = xslices.GrowJust(fields, len(ctxhooks)+1)
// Pass context through hooks.
for _, hook := range ctxhooks {
@ -423,7 +423,7 @@ func logf(ctx context.Context, depth int, lvl LEVEL, fields []kv.Field, s string
}
if s != "" {
// Append message as final log field.
// Append message (if given) as final log field.
fields = xslices.AppendJust(fields, kv.Field{
K: "msg", V: fmt.Sprintf(s, a...),
})