Skip to content

Commit

Permalink
#411 Add FieldsExclude parameter to console writer (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
choover-broad committed Feb 27, 2022
1 parent 588a61c commit 263b0bd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions console.go
Expand Up @@ -63,6 +63,9 @@ type ConsoleWriter struct {
// PartsExclude defines parts to not display in output.
PartsExclude []string

// FieldsExclude defines contextual fields to not display in output.
FieldsExclude []string

FormatTimestamp Formatter
FormatLevel Formatter
FormatCaller Formatter
Expand Down Expand Up @@ -137,6 +140,17 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer) {
var fields = make([]string, 0, len(evt))
for field := range evt {
var isExcluded bool
for _, excluded := range w.FieldsExclude {
if field == excluded {
isExcluded = true
break
}
}
if isExcluded {
continue
}

switch field {
case LevelFieldName, TimestampFieldName, MessageFieldName, CallerFieldName:
continue
Expand Down
17 changes: 17 additions & 0 deletions console_test.go
Expand Up @@ -354,6 +354,23 @@ func TestConsoleWriterConfiguration(t *testing.T) {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})

t.Run("Sets FieldsExclude", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, FieldsExclude: []string{"foo"}}

evt := `{"level": "info", "message": "Foobar", "foo":"bar", "baz":"quux"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "<nil> INF Foobar baz=quux\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
}

func BenchmarkConsoleWriter(b *testing.B) {
Expand Down

0 comments on commit 263b0bd

Please sign in to comment.