Skip to content

Commit

Permalink
Better test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
veqryn committed Apr 1, 2024
1 parent 63a89a5 commit 9bebb1d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ In that same workflow, the `HandlerOptions` and `AttrExtractor` types let us
extract any custom values from a context and have them automatically be
prepended or appended to all log lines using that context. By default, there are
extractors for anything added via `Prepend` and `Append`, but this repository
contains more optional Extractors that can be added:
contains some optional Extractors that can be added:
* `slogotel.ExtractTraceSpanID` extractor will automatically extract the OTEL
(OpenTelemetry) TraceID and SpanID, and add them to the log record, while also
annotating the Span with an error code if the log is at error level.
Expand Down
8 changes: 5 additions & 3 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ func TestHandler(t *testing.T) {

l := slog.New(h)

l = l.With("with1", "arg1", "with1", "arg2")
l = l.WithGroup("group1")
l = l.With("with1", "arg1", "with1", "arg2").With()
l = l.WithGroup("group1").WithGroup("")
l = l.With("with2", "arg1", "with2", "arg2")

l.Log(ctx, slog.LevelDebug-10, "ignored")
l.LogAttrs(ctx, slog.LevelDebug-10, "ignored")
l.InfoContext(ctx, "main message", "main1", "arg1", "main1", "arg2")

expectedText := `time=2023-09-29T13:00:59.000Z level=INFO msg="main message" prepend1=arg1 prepend1=arg2 prepend2=arg1 prepend2=arg2 with1=arg1 with1=arg2 group1.with2=arg1 group1.with2=arg2 group1.main1=arg1 group1.main1=arg2 group1.append1=arg1 group1.append1=arg2 group1.append2=arg1 group1.append2=arg2
Expand Down Expand Up @@ -75,7 +77,7 @@ func TestHandler(t *testing.T) {

if unmarshalled.Source.Function != "github.com/veqryn/slog-context.TestHandler" ||
!strings.HasSuffix(unmarshalled.Source.File, "slog-context/handler_test.go") ||
unmarshalled.Source.Line != 44 {
unmarshalled.Source.Line != 46 {
t.Errorf("Expected source fields are incorrect: %#+v\n", unmarshalled)
}
}
Expand Down
1 change: 1 addition & 0 deletions http/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestOutsideRequest(t *testing.T) {
ctx := slogctx.NewCtx(context.Background(), slog.New(h))

ctx = With(ctx, "id", "13579")
ctx = With(ctx) // Should be ignored

slogctx.Info(ctx, "utility method") // should also have "id"

Expand Down
4 changes: 2 additions & 2 deletions internal/test/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Handler struct {
}

// Enabled returns true
func (h *Handler) Enabled(context.Context, slog.Level) bool {
return true
func (h *Handler) Enabled(_ context.Context, lvl slog.Level) bool {
return lvl >= slog.LevelDebug
}

// Handle records a log record
Expand Down

0 comments on commit 9bebb1d

Please sign in to comment.