tfvars-atlantis-config/logger/logger.go

30 lines
565 B
Go
Raw Normal View History

2024-02-26 12:44:50 +00:00
package logger
import (
"context"
"go.uber.org/zap"
)
type loggerKey struct{}
// WithContext returns a new context with the logger added.
func WithContext(ctx context.Context, l *zap.Logger) context.Context {
return context.WithValue(ctx, loggerKey{}, l)
}
// FromContext returns the logger in the context if it exists, otherwise a new
// no-op logger is returned .
func FromContext(ctx context.Context) *zap.Logger {
if ctx == nil {
return zap.NewNop()
}
v := ctx.Value(loggerKey{})
if v == nil {
return zap.NewNop()
}
return v.(*zap.Logger)
}