...

Package clog

import "edge-infra.dev/pkg/lib/cli/clog"
Overview
Index
Examples

Overview ▾

Package clog implements github.com/go-logr/logr.Logger for CLIs.

func New

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:

// Add caller information to all logs
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

type MessageClass

MessageClass indicates which category or categories of messages to consider.

type MessageClass int
const (
    // None ignores all message classes.
    None MessageClass = iota
    // All considers all message classes.
    All
    // Info only considers info messages.
    Info
    // Error only considers error messages.
    Error
)

type Option

type Option func(*options)

func To

func To(dest io.Writer) Option

To allows setting the logging destination writer, defaults to `os.Stderr` if not provided

func WithCaller

func WithCaller(c MessageClass) Option

WithCaller configures which MessageClass logs get caller information.

func WithLevel

func WithLevel(lvl int) Option

WithLevel sets the verbosity level (V-level) for the created logr.Logger