...
Package clog
Package clog implements github.com/go-logr/logr.Logger for CLIs.
func New(opts ...Option) logr.Logger
New returns CLI friendly logr.Logger implementation.
▾ Example
Code:
log := New(To(os.Stdout))
log.Info("info message with default options")
log.Error(errSome, "error message with default options")
log.Info("invalid key", 42, "answer")
log.Info("missing value", "answer")
log.Info("logging some maps",
"map1", map[string]string{"key": "value", "op": "delete"},
"map2", map[string]string{"feeling": "less than map1", "key": "value"},
)
log.WithName("cli").WithName("subcommand").Info("coming from the deep", "kubectx", "ci-infra")
Output:
info message with default options
[error] <pkg/lib/cli/clog/example_test.go:13> error message with default options
err=some error
invalid key !(42)=answer
missing value answer=null
logging some maps
map1=
key value
op delete
map2=
feeling less than map1
key value
cli:subcommand: coming from the deep kubectx=ci-infra
▾ Example (WithCaller)
Code:
log := New(To(os.Stdout), WithCaller(All))
log.WithName("hello").WithName("world").Info("thanks for the fish")
Output:
hello:world: <pkg/lib/cli/clog/example_test.go:40> thanks for the fish
MessageClass indicates which category or categories of messages to consider.
type MessageClass int
const (
None MessageClass = iota
All
Info
Error
)
type Option func(*options)
func To(dest io.Writer) Option
To allows setting the logging destination writer, defaults to `os.Stderr` if
not provided
func WithCaller(c MessageClass) Option
WithCaller configures which MessageClass logs get caller information.
func WithLevel(lvl int) Option
WithLevel sets the verbosity level (V-level) for the created logr.Logger