-
Notifications
You must be signed in to change notification settings - Fork 599
Description
Bug Report: ConsoleWriter ignores PartsExclude and FormatExtra
Environment:
Go Version: go1.24.2
zerolog Version: v1.34.0
Description
When using zerolog.ConsoleWriter with custom fields specified in PartsOrder, the writer incorrectly prints the custom fields twice. It renders them once as part of PartsOrder and then again at the end of the log line.
I have tried to suppress these extra fields, such as adding them to the PartsExclude slice or providing a blank FormatExtra function. This makes it impossible to order custom fields before the message without producing duplicate output.
Steps to Reproduce
The following minimal, self-contained Go program demonstrates the issue. It configures ConsoleWriter to place two custom fields (pkg, fn) before the message and uses FormatExtra to attempt to suppress the duplicate fields that are printed by default.
// main.go
package main
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/rs/zerolog"
)
func main() {
// Configure ConsoleWriter to order custom fields before the message
output := zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "2006-01-02 15:04:05",
PartsOrder: []string{
zerolog.TimestampFieldName,
zerolog.LevelFieldName,
"pkg", // Custom field 1
"fn", // Custom field 2
zerolog.MessageFieldName,
},
// This blank function should suppress the duplicate fields at the end.
// NOTE: Using PartsExclude instead also fails to work.
FormatExtra: func(m map[string]interface{}, b *bytes.Buffer) error {
return nil
},
FormatTimestamp: func(i interface{}) string {
return fmt.Sprintf("A[%s]", i)
},
FormatLevel: func(i interface{}) string {
return fmt.Sprintf("B[%s]", strings.ToUpper(fmt.Sprintf("%s", i)))
},
FormatFieldValue: func(i interface{}) string {
return fmt.Sprintf("C[%s]", i)
},
FormatFieldName: func(i interface{}) string {
return ""
},
FormatMessage: func(i interface{}) string {
return fmt.Sprintf("%s", i)
},
}
logger := zerolog.New(output).With().Timestamp().Logger()
// Log a message with the custom fields
logger.Info().Str("pkg", "main").Str("fn", "TestFunc").Msg("This is a test.")
}
Actual Behavior
When running the code with go run main.go, the duplicate fields are still printed at the end of the line, indicating that FormatExtra (and PartsExclude) is being ignored.
A[2025-06-13T23:12:49Z] B[INFO] C[main] C[TestFunc] This is a test. C[TestFunc] C[main]
Expected Behavior
The FormatExtra function should have prevented the duplicate rendering of the pkg and fn fields, resulting in a clean, correctly ordered log line:
A[2025-06-13T23:12:49Z] B[INFO] C[main] C[TestFunc] This is a test.
However, I am a complete go noob (1 wk experience) so may be overlooking something completely obvious