Thanks to visit codestin.com
Credit goes to github.com

Skip to content

slogtest: demonstrate use of t.Context()#41

Merged
imjasonh merged 1 commit into
mainfrom
t-context
Aug 31, 2025
Merged

slogtest: demonstrate use of t.Context()#41
imjasonh merged 1 commit into
mainfrom
t-context

Conversation

@imjasonh
Copy link
Copy Markdown
Contributor

@imjasonh imjasonh commented Jul 8, 2025

Go 1.24 added t.Context(), which is presumably more idiomatic to use with clog than our own slogtest package.

This PR adds a comment mentioning t.Context and demonstrates how to get t.Context to have the same behavior as slogtest.Context(t).

Without the slog.SetDefault the behavior is different:

go test -trimpath ./slogtest -run=SlogTest -v             
=== RUN   TestSlogTest
    slogtest.go:36: level=INFO source=github.com/chainguard-dev/clog/slogtest/slogtest_test.go:13 msg="hello world" foo=bar
    slogtest.go:36: level=INFO source=github.com/chainguard-dev/clog/slogtest/slogtest_test.go:14 msg="me again" bar=baz
    slogtest.go:36: level=INFO source=github.com/chainguard-dev/clog/slogtest/slogtest_test.go:15 msg="okay last one" baz=true
    slogtest.go:36: level=DEBUG source=github.com/chainguard-dev/clog/slogtest/slogtest_test.go:17 msg="hello debug"
    slogtest.go:36: level=INFO source=github.com/chainguard-dev/clog/slogtest/slogtest_test.go:18 msg="hello info"
    slogtest.go:36: level=WARN source=github.com/chainguard-dev/clog/slogtest/slogtest_test.go:19 msg="hello warn"
    slogtest.go:36: level=ERROR source=github.com/chainguard-dev/clog/slogtest/slogtest_test.go:20 msg="hello error"
    slogtest.go:36: level=INFO source=github.com/chainguard-dev/clog/slogtest/another_test.go:9 msg="hello from fn" foo=bar
--- PASS: TestSlogTest (0.00s)
=== RUN   TestSlogTestTContext
2025/07/08 12:29:28 INFO hello world foo=bar
2025/07/08 12:29:28 INFO me again bar=baz
2025/07/08 12:29:28 INFO okay last one baz=true
2025/07/08 12:29:28 INFO hello info
2025/07/08 12:29:28 WARN hello warn
2025/07/08 12:29:28 ERROR hello error
2025/07/08 12:29:28 INFO hello from fn foo=bar
--- PASS: TestSlogTestTContext (0.00s)
PASS
ok      github.com/chainguard-dev/clog/slogtest 0.211s

(debug logs are dropped, no file paths or line numbers, timestamps are added)

I don't know whether we should officially deprecate slogtest in favor of clog.FromContext(t.Context()), but at least this documents an off-ramp.

@imjasonh imjasonh requested review from Copilot and wlynch July 8, 2025 16:32
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR documents and demonstrates using Go 1.24’s t.Context() with clog instead of the legacy slogtest.Context, and updates the slogtest package accordingly.

  • Add TestSlogTestTContext to show how to configure the default slog logger for use with t.Context().
  • Extend the slogtest package: bump the Logger interface to include Context(), update Context to wrap t.Context(), and update package docs.
  • Bump the module’s Go version directive to 1.24.4.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
slogtest/slogtest_test.go Added new test demonstrating t.Context() usage and imported log/slog.
slogtest/slogtest.go Extended Logger interface with Context(), updated Context func, and added docs.
slogtest/another_test.go Added fn helper to show context-based logging propagation.
go.mod Updated Go directive from 1.22.4 to 1.24.4.
Comments suppressed due to low confidence (1)

slogtest/slogtest.go:46

  • The code refers to context.Context but the context package isn't imported. Please add import "context" to the import block.
	Context() context.Context

@imjasonh imjasonh marked this pull request as ready for review August 14, 2025 18:14
@imjasonh imjasonh merged commit 5a063eb into main Aug 31, 2025
6 checks passed
@imjasonh imjasonh deleted the t-context branch August 31, 2025 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants