Skip to content

Add support for leveled fields #172

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions internal/entryhuman/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func formatValue(v interface{}) string {
return fmt.Sprintf("error calling Value: %v", err)
}
}
if _, ok := v.(slog.Map); ok {
byt, err := json.Marshal(v)
if err != nil {
panic(err)
}
return string(byt)
}
if v == nil {
return "<nil>"
}
Expand Down
2 changes: 1 addition & 1 deletion internal/entryhuman/testdata/object.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0001-01-01 00:00:00.000 [warn] obj="[{Name:obj1 Value:{foo:1 bar:2 dra:[98 108 97 104]}} {Name:obj2 Value:{foo:3 bar:4 dra:[98 108 97 104]}}]" map={"key1":"value1"}
0001-01-01 00:00:00.000 [warn] obj={"obj1":"{foo:1 bar:2 dra:[98 108 97 104]}","obj2":"{foo:3 bar:4 dra:[98 108 97 104]}"} map={"key1":"value1"}
27 changes: 24 additions & 3 deletions slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ func (l Logger) Log(ctx context.Context, e SinkEntry) {
return
}

e.Fields = l.fields.append(e.Fields)
filteredFields := make(Map, 0, len(e.Fields))
for _, fs := range l.fields.append(e.Fields) {
if fs.LevelFilter != nil && !fs.LevelFilter(l.level) {
continue
}
filteredFields = append(filteredFields, fs)
}
e.Fields = filteredFields
e.LoggerNames = appendNames(l.names, e.LoggerNames...)

for _, s := range l.sinks {
Expand Down Expand Up @@ -227,8 +234,22 @@ func appendNames(names []string, names2 ...string) []string {

// Field represents a log field.
type Field struct {
Name string
Value interface{}
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`

// LevelFilter, if set, instructs the logger to only process this field
// if it processing entries at the given level.
LevelFilter func(Level) bool `json:"-"`
}

// DebugF is a convenience constructor for Field, with LevelFilter only
// allowing Debug level entries.
func DebugF(name string, value interface{}) Field {
f := F(name, value)
f.LevelFilter = func(l Level) bool {
return l == LevelDebug
}
return f
}

// F is a convenience constructor for Field.
Expand Down
17 changes: 17 additions & 0 deletions slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ func TestLogger(t *testing.T) {
assert.Equal(t, "sinks", s1, s2)
})

t.Run("levelFilter", func(t *testing.T) {
t.Parallel()

s := &fakeSink{}
l := slog.Make(s)

l.Info(bg, "wow", slog.Error(io.EOF), slog.DebugF("nosee", "me"))

assert.Len(t, "entries", 1, s.entries)
assert.Len(t, "fields", 1, s.entries[0].Fields)

l = l.Leveled(slog.LevelDebug)
l.Info(bg, "wow", slog.Error(io.EOF), slog.DebugF("nosee", "me"))
assert.Len(t, "entries", 2, s.entries)
assert.Len(t, "fields", 2, s.entries[1].Fields)
})

t.Run("helper", func(t *testing.T) {
t.Parallel()

Expand Down