diff --git a/console.go b/console.go index a3fb542c..25c178bb 100644 --- a/console.go +++ b/console.go @@ -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 @@ -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 diff --git a/console_test.go b/console_test.go index 63a7ed18..1f863109 100644 --- a/console_test.go +++ b/console_test.go @@ -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 := " 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) {