Stitch any CI into Tangled
1package main
2
3// Tiny helpers for stashing a *slog.Logger on a context.Context. The stdlib
4// doesn't define a context key for slog, so by convention every app rolls
5// its own — the unexported key type ensures we can't collide with anyone
6// else's value, and FromContext falls back to slog.Default() so callers
7// don't need to special-case a missing logger.
8
9import (
10 "context"
11 "log/slog"
12)
13
14// loggerCtxKey is unexported so only this package can write to the slot.
15type loggerCtxKey struct{}
16
17// loggerInto returns a copy of ctx that carries logger.
18func loggerInto(ctx context.Context, logger *slog.Logger) context.Context {
19 return context.WithValue(ctx, loggerCtxKey{}, logger)
20}
21
22// loggerFrom retrieves the logger attached with loggerInto, or slog.Default
23// if none has been set.
24func loggerFrom(ctx context.Context) *slog.Logger {
25 if ctx == nil {
26 return slog.Default()
27 }
28 if l, ok := ctx.Value(loggerCtxKey{}).(*slog.Logger); ok {
29 return l
30 }
31 return slog.Default()
32}