From 6601b628294e5a053d386c1d075f60e4d655d306 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 1 Apr 2024 00:52:06 -0600 Subject: [PATCH] Better test coverage --- README.md | 2 +- ctx_test.go | 7 +++++++ handler_test.go | 4 ++-- http/middleware_test.go | 1 + internal/test/handler.go | 4 ++-- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1c79a89..f73bd78 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/ctx_test.go b/ctx_test.go index 99d969a..1d1a822 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -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"}} diff --git a/handler_test.go b/handler_test.go index b8e410f..3f4673d 100644 --- a/handler_test.go +++ b/handler_test.go @@ -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") diff --git a/http/middleware_test.go b/http/middleware_test.go index c538ba0..7f38988 100644 --- a/http/middleware_test.go +++ b/http/middleware_test.go @@ -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" diff --git a/internal/test/handler.go b/internal/test/handler.go index 7008b1a..de4307c 100644 --- a/internal/test/handler.go +++ b/internal/test/handler.go @@ -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