Skip to content

Commit bf6e610

Browse files
Remove context stuff (#28)
1 parent 4f9e6dc commit bf6e610

File tree

8 files changed

+31
-73
lines changed

8 files changed

+31
-73
lines changed

docs/img/demo.gif

475 Bytes
Loading

docs/img/keys.gif

-265 Bytes
Loading

docs/img/prefix.gif

1.5 KB
Loading

examples/demo/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,30 @@ func main() {
1818
slog.Int("temp", 42),
1919
slog.Duration("time", 2*time.Minute),
2020
)
21+
2122
sleep()
23+
2224
logger.Info(
2325
"Choosing wine pairing",
2426
slog.Any("choices", []string{"merlot", "malbec", "rioja"}),
2527
)
28+
2629
sleep()
30+
2731
logger.Error("No malbec left!")
32+
2833
sleep()
29-
logger.Warn("Falling back to second choice", slog.String("fallback", "rioja"))
3034

31-
logger.Info("Eating steak", slog.String("cut", "sirloin"), slog.Bool("enjoying", true))
35+
logger.Warn(
36+
"Falling back to second choice",
37+
slog.String("fallback", "rioja"),
38+
)
39+
40+
logger.Info(
41+
"Eating steak",
42+
slog.String("cut", "sirloin"),
43+
slog.Bool("enjoying", true),
44+
)
3245
}
3346

3447
func sleep() {

examples/keys/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ func main() {
1818
slog.Duration("duration", 30*time.Second),
1919
slog.Int("number", 42),
2020
)
21+
2122
sleep()
2223

2324
sub := logger.With(slog.Bool("sub", true))
24-
sub.Info("Hello from the sub logger", slog.String("subkey", "yes"))
25+
26+
sub.Info(
27+
"Hello from the sub logger",
28+
slog.String("subkey", "yes"),
29+
)
2530
}
2631

2732
func sleep() {

examples/prefix/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,29 @@ func main() {
1414
logger := log.New(os.Stderr)
1515
prefixed := logger.Prefixed("http")
1616

17-
logger.Info("Calling GitHub API", slog.String("url", "https://api.github.com/"))
17+
logger.Info(
18+
"Calling GitHub API",
19+
slog.String("url", "https://api.github.com/"),
20+
)
21+
1822
sleep()
1923

2024
prefixed.Warn(
2125
"Slow endpoint",
2226
slog.String("endpoint", "users/slow"),
2327
slog.Duration("duration", 10*time.Second),
2428
)
29+
2530
sleep()
31+
2632
prefixed.Info(
2733
"Response from get repos",
2834
slog.Int("status", http.StatusOK),
2935
slog.Duration("duration", 500*time.Millisecond),
3036
)
37+
3138
sleep()
39+
3240
prefixed.Error(
3341
"Response from something else",
3442
slog.Int("status", http.StatusBadRequest),

log.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ package log // import "go.followtheprocess.codes/log"
1010

1111
import (
1212
"bytes"
13-
"context"
1413
"io"
1514
"log/slog"
16-
"os"
1715
"slices"
1816
"strconv"
1917
"strings"
@@ -37,12 +35,6 @@ const (
3735
errorStyle = hue.Red | hue.Bold
3836
)
3937

40-
// ctxKey is the unexported type used for context key so this key never collides with another.
41-
type ctxKey struct{}
42-
43-
// contextKey is the actual key used to store and retrieve a Logger from a Context.
44-
var contextKey = ctxKey{}
45-
4638
// Logger is a command line logger. It is safe to use across concurrently
4739
// executing goroutines.
4840
type Logger struct {
@@ -78,25 +70,6 @@ func New(w io.Writer, options ...Option) *Logger {
7870
return logger
7971
}
8072

81-
// WithContext stores the given logger in a [context.Context].
82-
//
83-
// The logger may be retrieved from the context with [FromContext].
84-
func WithContext(ctx context.Context, logger *Logger) context.Context {
85-
return context.WithValue(ctx, contextKey, logger)
86-
}
87-
88-
// FromContext returns the [Logger] from a [context.Context].
89-
//
90-
// If the context does not contain a logger, a default logger is returned.
91-
func FromContext(ctx context.Context) *Logger {
92-
logger, ok := ctx.Value(contextKey).(*Logger)
93-
if !ok || logger == nil {
94-
return New(os.Stderr)
95-
}
96-
97-
return logger
98-
}
99-
10073
// With returns a new [Logger] with the given persistent key value pairs.
10174
//
10275
// The returned logger is otherwise an exact clone of the caller.

log_test.go

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ func TestVisual(t *testing.T) {
2121
hue.Enabled(true) // Force colour
2222

2323
logger := log.New(os.Stdout, log.WithLevel(log.LevelDebug))
24-
2524
prefixed := logger.Prefixed("cooking")
2625

2726
logger.Debug("Doing some debuggy things")
@@ -192,7 +191,7 @@ func TestRace(t *testing.T) {
192191
logger := log.New(buf, log.TimeFunc(fixedTime))
193192
sub := logger.Prefixed("sub")
194193

195-
const n = 5
194+
const n = 1000
196195

197196
var wg sync.WaitGroup
198197
wg.Add(n)
@@ -224,46 +223,6 @@ func TestRace(t *testing.T) {
224223
test.Equal(t, len(lines), n*2, test.Context("expected %d log lines", n*2))
225224
}
226225

227-
func TestContext(t *testing.T) {
228-
t.Run("present", func(t *testing.T) {
229-
buf := &bytes.Buffer{}
230-
231-
// Constantly return the same time
232-
fixedTime := func() time.Time {
233-
fixed, err := time.Parse(time.RFC3339, "2025-04-01T13:34:03Z")
234-
test.Ok(t, err)
235-
236-
return fixed
237-
}
238-
239-
// Configure it a bit so we know we're getting the right one
240-
logger := log.New(buf, log.TimeFunc(fixedTime), log.TimeFormat(time.Kitchen))
241-
242-
logger.Info("Before")
243-
244-
ctx := t.Context()
245-
246-
ctx = log.WithContext(ctx, logger)
247-
248-
after := log.FromContext(ctx)
249-
250-
after.Info("After")
251-
252-
got := buf.String()
253-
254-
test.Diff(t, got, "1:34PM INFO: Before\n1:34PM INFO: After\n")
255-
})
256-
257-
t.Run("missing", func(t *testing.T) {
258-
_, stderr := test.CaptureOutput(t, func() error {
259-
log.FromContext(t.Context()).Info("FromContext")
260-
return nil
261-
})
262-
263-
test.True(t, strings.Contains(stderr, "FromContext"))
264-
})
265-
}
266-
267226
func BenchmarkLogger(b *testing.B) {
268227
hue.Enabled(true) // Force colour
269228

0 commit comments

Comments
 (0)