File: context.go

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

import "context"

// WithContext wraps the given logger in context.
func WithContext(ctx context.Context, logger *Logger) context.Context {
	return context.WithValue(ctx, ContextKey, logger)
}

// FromContext returns the logger from the given context.
// This will return the default package logger if no logger
// found in context.
func FromContext(ctx context.Context) *Logger {
	if logger, ok := ctx.Value(ContextKey).(*Logger); ok {
		return logger
	}
	return Default()
}

type contextKey struct{ string }

// ContextKey is the key used to store the logger in context.
var ContextKey = contextKey{"log"}