File: context.go

package info (click to toggle)
golang-github-zitadel-logging 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 156 kB
  • sloc: javascript: 8; makefile: 2
file content (23 lines) | stat: -rw-r--r-- 500 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package logging

import (
	"context"

	"log/slog"
)

type ctxKeyType struct{}

var ctxKey ctxKeyType

// FromContext takes a Logger from the context, if it was
// previously set by [ToContext]
func FromContext(ctx context.Context) (logger *slog.Logger, ok bool) {
	logger, ok = ctx.Value(ctxKey).(*slog.Logger)
	return logger, ok
}

// ToContext sets a Logger to the context.
func ToContext(ctx context.Context, logger *slog.Logger) context.Context {
	return context.WithValue(ctx, ctxKey, logger)
}