Skip to content

Commit

Permalink
Allow to exclude parts from console output (#272)
Browse files Browse the repository at this point in the history
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max and crazy-max committed Nov 29, 2020
1 parent cac3894 commit 29d8dac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions console.go
Expand Up @@ -57,6 +57,9 @@ type ConsoleWriter struct {
// PartsOrder defines the order of parts in output.
PartsOrder []string

// PartsExclude defines parts to not display in output.
PartsExclude []string

FormatTimestamp Formatter
FormatLevel Formatter
FormatCaller Formatter
Expand Down Expand Up @@ -208,6 +211,14 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) {
var f Formatter

if w.PartsExclude != nil && len(w.PartsExclude) > 0 {
for _, exclude := range w.PartsExclude {
if exclude == p {
return
}
}
}

switch p {
case LevelFieldName:
if w.FormatLevel == nil {
Expand Down
18 changes: 18 additions & 0 deletions console_test.go
Expand Up @@ -336,6 +336,24 @@ func TestConsoleWriterConfiguration(t *testing.T) {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})

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

d := time.Unix(0, 0).UTC().Format(time.RFC3339)
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "INF Foobar\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 29d8dac

Please sign in to comment.