logging

package
v0.104.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 11 Imported by: 20

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitFlags

func InitFlags(flagset *flag.FlagSet)

func NewContext

func NewContext(ctx context.Context, log Logger) context.Context

NewContext is a wrapper for logr.NewContext.

func NewContextWithDiscard

func NewContextWithDiscard(ctx context.Context) context.Context

NewContextWithDiscard adds a discard logger to the given context and returns the new context.

func PreventKeyConflicts

func PreventKeyConflicts(log logr.Logger) logr.Logger

PreventKeyConflicts takes a logr.Logger and wraps a KeyConflictPreventionLayer around its LogSink. It is already used by the logging framework's constructors and will likely not have to be called from outside the package. Mainly exported for testing purposes.

func SetLogger

func SetLogger(log Logger)

Types

type Config

type Config struct {
	Development       bool
	Cli               bool
	DisableStacktrace bool
	DisableCaller     bool
	DisableTimestamp  bool
	Level             logLevelValue
	Format            logFormatValue
	// contains filtered or unexported fields
}

func (*Config) SetDisableCaller

func (c *Config) SetDisableCaller(zapCfg *zap.Config)

SetDisableCaller dis- or enables the caller according to the provided flag if the flag was provided

func (*Config) SetDisableStacktrace

func (c *Config) SetDisableStacktrace(zapCfg *zap.Config)

SetDisableStacktrace dis- or enables the stackstrace according to the provided flag if the flag was provided

func (*Config) SetLogFormat

func (c *Config) SetLogFormat(zapCfg *zap.Config)

SetLogFormat sets the logging format according to the provided flag if the flag was provided

func (*Config) SetLogLevel

func (c *Config) SetLogLevel(zapCfg *zap.Config)

SetLogLevel sets the logging verbosity according to the provided flag if the flag was provided

func (*Config) SetTimestamp

func (c *Config) SetTimestamp(zapCfg *zap.Config)

SetTimestamp dis- or enables the logging of timestamps according to the provided flag if the flag was provided

func (*Config) WithLogFormat

func (c *Config) WithLogFormat(f LogFormat) *Config

func (*Config) WithLogLevel

func (c *Config) WithLogLevel(l LogLevel) *Config

type KeyConflictPreventionLayer

type KeyConflictPreventionLayer struct {
	logr.LogSink
	// contains filtered or unexported fields
}

KeyConflictPreventionLayer is a helper struct. It implements logr.LogSink by containing a LogSink internally, to which all method calls are forwarded. The only purpose of this struct is to detect duplicate keys for logr.WithValues and replace them to avoid conflicts.

func (KeyConflictPreventionLayer) Enabled

func (kcpl KeyConflictPreventionLayer) Enabled(level int) bool

func (KeyConflictPreventionLayer) Error

func (kcpl KeyConflictPreventionLayer) Error(err error, msg string, keysAndValues ...interface{})

func (KeyConflictPreventionLayer) Info

func (kcpl KeyConflictPreventionLayer) Info(level int, msg string, keysAndValues ...interface{})

func (KeyConflictPreventionLayer) Init

func (KeyConflictPreventionLayer) WithName

func (kcpl KeyConflictPreventionLayer) WithName(name string) logr.LogSink

func (KeyConflictPreventionLayer) WithValues

func (kcpl KeyConflictPreventionLayer) WithValues(keysAndValues ...interface{}) logr.LogSink

WithValues works as usual, but it will replace keys which already exist with a suffixed version indicating the conflict.

type LogFormat

type LogFormat int
const (
	TEXT LogFormat
	JSON
)

func ParseLogFormat

func ParseLogFormat(raw string) (LogFormat, error)

func (LogFormat) String

func (f LogFormat) String() string

type LogLevel

type LogLevel int
const (
	ERROR LogLevel
	INFO
	DEBUG
)

func ParseLogLevel

func ParseLogLevel(raw string) (LogLevel, error)

func (LogLevel) String

func (l LogLevel) String() string

type Logger

type Logger struct {
	// contains filtered or unexported fields
}
var (
	Log Logger
)

func Discard

func Discard() Logger

Discard is a wrapper for logr.Discard.

func FromContext

func FromContext(ctx context.Context) (Logger, error)

FromContext wraps the result of logr.FromContext into a logging.Logger.

func FromContextOrDiscard

func FromContextOrDiscard(ctx context.Context) Logger

FromContextOrDiscard works like FromContext, but it will return a discard logger if no logger is found in the context.

func FromContextOrNew

func FromContextOrNew(ctx context.Context, keysAndValuesFallback []interface{}, keysAndValues ...interface{}) (Logger, context.Context)

FromContextOrNew tries to fetch a logger from the context. It is expected that a logger is contained in the context. If retrieving it fails, a new logger will be created and an error is logged. keysAndValuesFallback contains keys and values which will only be added if the logger could not be retrieved and a new one had to be created. The key-value-pairs from keysAndValues will always be added. A new context, containing the created logger, will be returned. The function panics if the logger cannot be fetched from the context and creating a new one fails.

func FromContextOrPanic

func FromContextOrPanic(ctx context.Context) Logger

FromContextOrPanic tries to fetch a logger from the context. If that fails, the function panics.

func FromContextWithFallback

func FromContextWithFallback(ctx context.Context, fallback Logger, keysAndValues ...interface{}) (Logger, context.Context)

FromContextWithFallback tries to fetch a logger from the context. If that fails, the provided fallback logger is used instead. It returns the fetched logger, enriched with the given key-value-pairs, and a context containing this new logger.

func GetLogger

func GetLogger() (Logger, error)

GetLogger returns a singleton logger. Will initialize a new logger, if it doesn't exist yet.

func MustStartReconcileFromContext

func MustStartReconcileFromContext(ctx context.Context, req reconcile.Request, keysAndValuesFallback []interface{}, keysAndValues ...interface{}) (Logger, context.Context)

MustStartReconcileFromContext is a helper function for easily getting a logger at the beginning of a reconciliation. It is basically a combination of FromContextOrNew, which tries to fetch the logger from the context, or creates a new one, if that fails, and StartReconcileAndAddToContext, which logs the beginning of the reconciliation and returns a new context containing the configured logger.

func New

func New(config *Config) (Logger, error)

func NewCliLogger

func NewCliLogger() (Logger, error)

NewCliLogger creates a new logger for cli usage. CLI usage means that by default: - encoding is console - timestamps are disabled (can be still activated by the cli flag) - level are color encoded

func StartReconcileFromContext

func StartReconcileFromContext(ctx context.Context, req reconcile.Request) (Logger, context.Context, error)

StartReconcileFromContext fetches the logger from the context and adds the reconciled resource. It also logs a 'start reconcile' message. The returned context contains the enriched logger.

func Wrap

func Wrap(log logr.Logger) Logger

Wrap constructs a new Logger, using the provided logr.Logger internally.

func (Logger) Debug

func (l Logger) Debug(msg string, keysAndValues ...interface{})

Debug logs a message at DEBUG level.

func (Logger) Enabled

func (l Logger) Enabled(lvl LogLevel) bool

Enabled tests whether logging at the provided level is enabled. This deviates from the logr Enabled() function, which doesn't take an argument.

func (Logger) Error

func (l Logger) Error(err error, msg string, keysAndValues ...interface{})

Error logs an error, with the given message and key/value pairs as context. It functions similarly to calling Info with the "error" named value, but may have unique behavior, and should be preferred for logging errors (see the package documentations for more information).

The msg field should be used to add context to any underlying error, while the err field should be used to attach the actual error that triggered this log line, if present.

func (Logger) Info

func (l Logger) Info(msg string, keysAndValues ...interface{})

Info logs a non-error message with the given key/value pairs as context.

The msg argument should be used to add some constant description to the log line. The key/value pairs can then be used to add additional variable information. The key/value pairs should alternate string keys and arbitrary values.

func (Logger) IsInitialized

func (l Logger) IsInitialized() bool

IsInitialized returns true if the logger is ready to be used and false if it is an 'empty' logger (e.g. created by Logger{}).

func (Logger) Log

func (l Logger) Log(lvl LogLevel, msg string, keysAndValues ...interface{})

Log logs at the given log level. It can be used to log at dynamically determined levels.

func (Logger) Logr

func (l Logger) Logr() logr.Logger

Logr returns the internal logr.Logger.

func (Logger) Reconciles

func (l Logger) Reconciles(name, reconciledResource string) Logger

Reconciles is meant to be used for the logger initialization for controllers. It is a wrapper for WithName(name).WithValues(lc.KeyReconciledResourceKind, reconciledResource).

func (Logger) StartReconcile

func (l Logger) StartReconcile(req reconcile.Request, keysAndValues ...interface{}) Logger

StartReconcile works like StartReconcileFromContext, but it is called on an existing logger instead of fetching one from the context.

func (Logger) StartReconcileAndAddToContext

func (l Logger) StartReconcileAndAddToContext(ctx context.Context, req reconcile.Request, keysAndValues ...interface{}) (Logger, context.Context)

StartReconcileAndAddToContext works like StartReconcile, but additionally returns a context with the new logger.

func (Logger) WithName

func (l Logger) WithName(name string) Logger

WithName adds a new element to the logger's name. Successive calls with WithName continue to append suffixes to the logger's name. It's strongly recommended that name segments contain only letters, digits, and hyphens (see the package documentation for more information).

func (Logger) WithNameAndContext

func (l Logger) WithNameAndContext(ctx context.Context, name string) (Logger, context.Context)

WithNameAndContext works like WithName, but also adds the logger directly to a context and returns the new context.

func (Logger) WithValues

func (l Logger) WithValues(keysAndValues ...interface{}) Logger

WithValues adds some key-value pairs of context to a logger. See Info for documentation on how key/value pairs work.

func (Logger) WithValuesAndContext

func (l Logger) WithValuesAndContext(ctx context.Context, keysAndValues ...interface{}) (Logger, context.Context)

WithValuesAndContext works like WithValues, but also adds the logger directly to a context and returns the new context.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL