Skip to content

Commit

Permalink
Get better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
veqryn committed Nov 16, 2023
1 parent 3d58082 commit e769096
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
11 changes: 9 additions & 2 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ func TestCtx(t *testing.T) {
},
})

ctx := ToCtx(context.Background(), slog.New(h))
// Confirm Logger retrieves default if nothing stored in ctx yet
l := slog.New(h)
slog.SetDefault(l)
if Logger(context.Background()) != l {
t.Error("Expecting default logger retrieved")
}

ctx := ToCtx(context.Background(), slog.Default())

ctx = With(ctx, "with1", "arg1", "with1", "arg2")
ctx = With(ctx, "with2", "arg1", "with2", "arg2")
Expand All @@ -38,7 +45,7 @@ func TestCtx(t *testing.T) {
With(ctx, "with6", "arg1", "with6", "arg2") // Ensure we aren't overwriting the parent context

// Test with getting logger back out
l := Logger(ctx)
l = Logger(ctx)
l.InfoContext(ctx, "main message", "main1", "arg1", "main1", "arg2")
expectedInfo := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","with1":"arg1","with1":"arg2","with2":"arg1","with2":"arg2","group1":{"with4":"arg1","with4":"arg2","with5":"arg1","with5":"arg2","main1":"arg1","main1":"arg2"}}
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func main() {
ctx = slogcontext.Prepend(ctx, "prependKey", "prependValue")

// Append some slog attributes to the end of future log lines:
ctx = slogcontext.Append(ctx, "appendKey", "appendValue")
// Prepend and Append have the same args signature as slog methods,
// and can take a mix of slog.Attr and key-value pairs.
ctx = slogcontext.Append(ctx, slog.String("appendKey", "appendValue"))

// Use the logger like normal:
slog.WarnContext(ctx, "main message", "mainKey", "mainValue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func main() {
// Create a group directly on the logger in the context:
ctx = slogcontext.WithGroup(ctx, "someGroup")

ctx = slogcontext.With(ctx, "subKey", "subValue")
// With and wrapper methods have the same args signature as slog methods,
// and can take a mix of slog.Attr and key-value pairs.
ctx = slogcontext.With(ctx, slog.String("subKey", "subValue"))

// Access the logger in the context directly with handy wrappers for Debug/Info/Warn/Error/Log/LogAttrs:
slogcontext.Info(ctx, "main message", "mainKey", "mainValue")
Expand Down
7 changes: 3 additions & 4 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package slogcontext

import (
"context"
"log/slog"
"testing"
)
Expand All @@ -12,13 +11,13 @@ func TestHandler(t *testing.T) {
tester := &testHandler{}
h := NewHandler(tester, nil)

ctx := context.Background()
ctx = Prepend(ctx, "prepend1", "arg1", "prepend1", "arg2")
ctx := Prepend(nil, "prepend1", "arg1", slog.String("prepend1", "arg2"))
ctx = Prepend(ctx, "prepend2", "arg1", "prepend2", "arg2")
Prepend(ctx, "prepend3", "arg1", "prepend3", "arg2") // Ensure we aren't overwriting the parent context
ctx = Append(ctx, "append1", "arg1", "append1", "arg2")
ctx = Append(ctx, "append2", "arg1", "append2", "arg2")
ctx = Append(ctx, slog.String("append2", "arg1"), "append2", "arg2")
Append(ctx, "append3", "arg1", "append3", "arg2") // Ensure we aren't overwriting the parent context
Append(nil, "append4", "arg1", "append4", "arg2") // Ensure we aren't overwriting the parent context

l := slog.New(h)

Expand Down

0 comments on commit e769096

Please sign in to comment.