Skip to content

Commit

Permalink
Add FieldsOrder functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mAdkins committed Mar 2, 2024
1 parent bd28965 commit b32f95a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
38 changes: 38 additions & 0 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ type ConsoleWriter struct {
// PartsExclude defines parts to not display in output.
PartsExclude []string

// FieldsOrder defines the order of contextual fields in output.
FieldsOrder []string

fieldIsOrdered map[string]bool

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

Expand Down Expand Up @@ -183,10 +188,15 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
case LevelFieldName, TimestampFieldName, MessageFieldName, CallerFieldName:
continue
}

fields = append(fields, field)
}
sort.Strings(fields)

if len(w.FieldsOrder) > 0 {
fields = w.orderFields(fields)
}

// Write space only if something has already been written to the buffer, and if there are fields.
if buf.Len() > 0 && len(fields) > 0 {
buf.WriteByte(' ')
Expand Down Expand Up @@ -318,6 +328,34 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
}
}

// orderFields takes an array of field names and an array representing field order
// and returns an array with any ordered fields at the beginning, in order,
// and the remaining fields after in their original order.
func (w ConsoleWriter) orderFields(fields []string) []string {
if w.fieldIsOrdered == nil {
w.fieldIsOrdered = make(map[string]bool)
for _, fieldName := range w.FieldsOrder {
w.fieldIsOrdered[fieldName] = true
}
}
hasOrderedField := make(map[string]bool)
otherFields := make([]string, 0, len(fields))
for _, fieldName := range fields {
if w.fieldIsOrdered[fieldName] {
hasOrderedField[fieldName] = true
} else {
otherFields = append(otherFields, fieldName)
}
}
result := make([]string, 0, len(fields))
for _, fieldName := range w.FieldsOrder {
if hasOrderedField[fieldName] {
result = append(result, fieldName)
}
}
return append(result, otherFields...)
}

// needsQuote returns true when the string s should be quoted in output.
func needsQuote(s string) bool {
for i := range s {
Expand Down
17 changes: 17 additions & 0 deletions console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,23 @@ func TestConsoleWriterConfiguration(t *testing.T) {
}
})

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

evt := `{"level": "info", "message": "Zoo", "aardvark": "Able", "mussel": "Mountain", "zebra": "Zulu"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "<nil> INF Zoo zebra=Zulu aardvark=Able mussel=Mountain\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
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"}}
Expand Down

0 comments on commit b32f95a

Please sign in to comment.