-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
40 lines (35 loc) · 1.12 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package altnrslog
import (
"context"
"errors"
"log/slog"
)
type loggerKey struct{}
var (
// ErrInvalidHandler is returned when the handler is not a TransactionalHandler.
ErrInvalidHandler = errors.New("invalid handler")
// ErrNotStored is returned when the logger is not stored in context.Context.
ErrNotStored = errors.New("logger not stored")
)
// FromContext returns [*slog.Logger] with [*TransactionalHandler], stored in the context.Context.
// If it does not exist, return [ErrNotStored].
func FromContext(ctx context.Context) (*slog.Logger, error) {
logger, ok := ctx.Value(loggerKey{}).(*slog.Logger)
if !ok {
return nil, ErrNotStored
}
_, ok = logger.Handler().(*TransactionalHandler)
if !ok {
return nil, ErrNotStored
}
return logger, nil
}
// StoreToContext stores the [*slog.Logger] in [context.Context].
// Logger must be set to [*TransactionalHandler] in the Handler
func StoreToContext(ctx context.Context, logger *slog.Logger) (context.Context, error) {
_, ok := logger.Handler().(*TransactionalHandler)
if !ok {
return ctx, ErrInvalidHandler
}
return context.WithValue(ctx, loggerKey{}, logger), nil
}