Skip to content

Commit

Permalink
more WithXXX
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 19, 2024
1 parent 4f80b90 commit 3f1328d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion slog/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,11 +975,17 @@ func (s *Entry) leadingTags(roughSize int, lvl Level, stackFrame uintptr, msg st
return
}

func (s *Entry) WithContextKeys(keys ...any) *Entry {
func (s *Entry) SetContextKeys(keys ...any) *Entry {
s.contextKeys = append(s.contextKeys, keys...)
return s
}

func (s *Entry) WithContextKeys(keys ...any) *Entry {
l := s.newChildLogger()
l.SetContextKeys(keys...)
return l
}

func (s *Entry) ctxKeysWanted() bool { return len(s.contextKeys) > 0 }
func (s *Entry) ctxKeys() []any { return s.contextKeys }
func (s *Entry) fromCtx(ctx context.Context) (ret Attrs) {
Expand Down
4 changes: 4 additions & 0 deletions slog/i.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ type (
SetAttrs1(attrs Attrs) *Entry //
Set(args ...any) *Entry // key1,val1,key2,val2,.... Of course, Attr, Attrs in args will be recognized as is

SetContextKeys(keys ...any) *Entry // given keys will be tried extracting from context.Context automatically
WithContextKeys(keys ...any) *Entry // given keys will be tried extracting from context.Context automatically

SetWriter(wr io.Writer) *Entry // use the given writer
WithWriter(wr io.Writer) *Entry // use the given writer
AddWriter(wr io.Writer) *Entry // append more writers via this interface
SetErrorWriter(wr io.Writer) *Entry //
WithErrorWriter(wr io.Writer) *Entry //
AddErrorWriter(wr io.Writer) *Entry //
ResetWriters() *Entry //
GetWriter() (wr LogWriter) // return level-matched writer
Expand Down
2 changes: 1 addition & 1 deletion slog/i_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func TestSlogNewWithAttrs(t *testing.T) {
func TestSlogWithContext(t *testing.T) {
logger := slog.New().WithAttrs(slog.String("app-version", "v0.0.1-beta"))
ctx := context.WithValue(context.Background(), "ctx", "oh,oh,oh") //nolint:staticcheck
logger.WithContextKeys("ctx").InfoContext(ctx, "info msg",
logger.SetContextKeys("ctx").InfoContext(ctx, "info msg",
"attr1", 111333,
slog.Group("memory",
slog.Int("current", 50),
Expand Down
2 changes: 1 addition & 1 deletion slog/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func TestWithWriter(t *testing.T) {
var ss strings.Builder
ss.WriteString("val")

l.WithContextKeys(&ss, "from") // set two keys here: one is a Stringer, another is a string
l.SetContextKeys(&ss, "from") // set two keys here: one is a Stringer, another is a string

ctx := context.WithValue(
context.WithValue(context.Background(), "from", "consul-center"),
Expand Down

0 comments on commit 3f1328d

Please sign in to comment.