rs / zerolog

Zero Allocation JSON Logger
MIT License
10.33k stars 564 forks source link

fix: correct field filtering logic in writeFields #680

Open yqaty opened 2 months ago

yqaty commented 2 months ago

ConsoleWriter.Write processes custom fields specified in PartsOrder to output only their values instead of the key-value pairs. In practice:

package main

import (
    "github.com/rs/zerolog"
)

func main() {
    cw := zerolog.NewConsoleWriter()
    cw.PartsOrder = append(cw.PartsOrder, "k1")
    logger := zerolog.New(cw).With().Timestamp().Logger()
    logger.Info().Str("k1", "v1").Msg("hello world")
}

Actual output:

5:09PM INF hello world v1 k1=v1

The field k1 is duplicated, and the expected output is:

5:09PM INF hello world v1

This is due to a bug in ConsoleWrite.writeFields where it filters fields based on the default PartsOrder rather than the current PartsOrder.