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 6601b62
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 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
7 changes: 7 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func TestCtx(t *testing.T) {
}

// Test with wrappers
buf.Reset()
Log(ctx, slog.LevelDebug-10, "ignored")
LogAttrs(ctx, slog.LevelDebug-10, "ignored")
if buf.String() != "" {
t.Errorf("Expected:\n%s\nGot:\n%s\n", "", buf.String())
}

buf.Reset()
Debug(ctx, "main message", "main1", "arg1", "main1", "arg2")
expectedDebug := `{"time":"2023-09-29T13:00:59Z","level":"DEBUG","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
4 changes: 2 additions & 2 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ 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.InfoContext(ctx, "main message", "main1", "arg1", "main1", "arg2")
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 6601b62

Please sign in to comment.