parent
588a61c2df
commit
263b0bde36
14
console.go
14
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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue