mirror of
https://github.com/3bbbeau/tfvars-atlantis-config.git
synced 2024-11-24 06:50:51 +00:00
30 lines
565 B
Go
30 lines
565 B
Go
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)
|
|
}
|