Skip to content

Commit

Permalink
feat: allow nested params with logus
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Jan 18, 2024
1 parent c560553 commit c030303
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 8 additions & 1 deletion goutils/logus_core/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func logGroupFiles() slog.Attr {

type SlogGroup struct {
Params map[string]string
Attrs []slog.Attr
}

func (s SlogGroup) Render() slog.Attr {
Expand All @@ -25,13 +26,19 @@ func (s SlogGroup) Render() slog.Attr {
anies = append(anies, value)
}

for _, value := range s.Attrs {
anies = append(anies, value)
}

return slog.Group("extras", anies...)
}

type SlogParam func(r *SlogGroup)

func newSlogGroup(opts ...SlogParam) slog.Attr {
client := &SlogGroup{Params: make(map[string]string)}
client := &SlogGroup{
Params: make(map[string]string),
}
for _, opt := range opts {
opt(client)
}
Expand Down
17 changes: 17 additions & 0 deletions goutils/logus_core/params_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logus_core

import (
"log/slog"
"testing"

"github.com/darklab8/darklab_goutils/goutils/logus_core/logus_types"
Expand All @@ -13,3 +14,19 @@ func TestSlogging(t *testing.T) {

logger.Debug("123", TestParam(456))
}

func NestedParam(value string) SlogParam {
return func(c *SlogGroup) {
c.Attrs = append(c.Attrs,
slog.Group("nested",
slog.Int("number", 1),
slog.String("smth", value),
),
)
}
}

func TestNested(t *testing.T) {
logger := NewLogger(LEVEL_DEBUG, logus_types.EnableJsonFormat(true), logus_types.EnableFileShowing(false))
logger.Debug("123", NestedParam("abc"))
}

0 comments on commit c030303

Please sign in to comment.