go-waku/logging/context.go

23 lines
498 B
Go
Raw Permalink Normal View History

2022-05-27 13:25:06 +00:00
package logging
import (
"context"
"go.uber.org/zap"
)
var logKey = &struct{}{}
2022-07-23 12:12:56 +00:00
// From allows retrieving the Logger from a Context.
// Returns nil if Context does not have one.
2022-05-27 13:25:06 +00:00
func From(ctx context.Context) *zap.Logger {
2022-07-23 12:12:56 +00:00
logger, _ := ctx.Value(logKey).(*zap.Logger)
return logger
2022-05-27 13:25:06 +00:00
}
// With associates a Logger with a Context to allow passing
// a logger down the call chain.
func With(ctx context.Context, log *zap.Logger) context.Context {
return context.WithValue(ctx, logKey, log)
}