Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add method to get event fields inside a Hook #682

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package zerolog

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -828,3 +830,21 @@ func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event {
e.buf = enc.AppendMACAddr(enc.AppendKey(e.buf, key), ha)
return e
}

// GetMetadata returns the JSON decoded metadata of the event. Please note that the metadata might not be mapped to their original field type as JSON decoder maps numbers to float64, etc.
func (e *Event) GetMetadata() (map[string]interface{}, error) {
if e == nil {
return nil, nil
}

eventFields := make(map[string]interface{})
buffer := bytes.TrimSpace(e.buf)
// If `Msg()` was not called the buffer will be missing the closing curly brace
if !bytes.HasSuffix(buffer, []byte("}")) {
erezrokah marked this conversation as resolved.
Show resolved Hide resolved
erezrokah marked this conversation as resolved.
Show resolved Hide resolved
buffer = append(buffer, '}')
}
if err := json.Unmarshal(buffer, &eventFields); err != nil {
return nil, err
}
return eventFields, nil
}
61 changes: 61 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !binary_log
// +build !binary_log

package zerolog
Expand Down Expand Up @@ -63,3 +64,63 @@ func TestEvent_EmbedObjectWithNil(t *testing.T) {
t.Errorf("Event.EmbedObject() = %q, want %q", got, want)
}
}

func TestEvent_GetFields(t *testing.T) {
erezrokah marked this conversation as resolved.
Show resolved Hide resolved
type testCase struct {
name string
e *Event
message string
want map[string]interface{}
}

testCases := []testCase{
{
name: "event without message",
e: newEvent(nil, DebugLevel).Str("foo", "bar").Float64("n", 42),
want: map[string]interface{}{
"foo": "bar",
"n": float64(42),
},
},
{
name: "event without message and integer",
e: newEvent(nil, DebugLevel).Str("foo", "bar").Int("n", 42),
want: map[string]interface{}{
"foo": "bar",
"n": float64(42),
},
},
{
name: "event with message",
e: newEvent(nil, DebugLevel).Str("foo", "bar").Float64("n", 42),
message: "test",
want: map[string]interface{}{
"foo": "bar",
"n": float64(42),
"message": "test",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.message != "" {
tc.e.Msg(tc.message)
}
got, err := tc.e.GetMetadata()
if err != nil {
t.Error(err)
}

if len(got) != len(tc.want) {
t.Errorf("Event.GetFields() = %v, want %v", len(got), len(tc.want))
}
for k, v := range tc.want {
if got[k] != v {
t.Errorf("Event.GetFields() = %v, want %v", got[k], v)
}
}
})

}
}