Documentation
¶
Index ¶
- Constants
- func FromContext(ctx context.Context) *logr.Logger
- func Get(logLevel int8) *logr.Logger
- func GetGlobalLogger() *logr.Logger
- func GetNoopLogger() *logr.Logger
- func GetWithOptions(opts Options) *logr.Logger
- func Sync()
- func WithLogger(ctx context.Context, log *logr.Logger) context.Context
- func WithValues(lgr *logr.Logger, keysAndValues ...any) *logr.Logger
- type LogFormat
- type Options
Constants ¶
const ( RootCommandKey = "root_command" SubCommandKey = "sub_command" CommitKey = "commit" VersionKey = "version" BuildTimeKey = "build_time" GoVersionKey = "go_version" TimeStampKey = "timestamp" MessageKey = "message" )
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext retrieves the logr.Logger from the context. If no logger is found in the context, it returns the globally configured logger. If Setup has not been called, it returns a no-op logger to prevent panics.
func Get ¶
Get initializes the global Zap and Logr loggers with default options. It can only be called once. Subsequent calls will have no effect. logLevel: The minimum logging level (-1=Debug, 0=Info, 1=Warn, 2=Error). This function must be called before using FromContext or any logging operations.
func GetGlobalLogger ¶
GetGlobalLogger returns the globally configured logr.Logger. This is useful for top-level logging in main where context might not be readily available, or as a fallback. It will return a no-op logger if Setup has not been called.
func GetNoopLogger ¶
func GetWithOptions ¶
GetWithOptions initializes the global Zap and Logr loggers with custom options. It can only be called once. Subsequent calls will have no effect. This function must be called before using FromContext or any logging operations.
func Sync ¶
func Sync()
Sync flushes any buffered log entries to their destination. This should be called before the application exits, typically via `defer logger.Sync()` in main.
func WithLogger ¶
WithLogger returns a new context with the provided logr.Logger attached. If the context already contains the same logger instance, it returns the original context. This allows logger propagation through context for structured logging.
func WithValues ¶
WithValues returns a new logr.Logger with additional key-value pairs for structured logging. The provided keysAndValues are added to the logger's context, allowing for richer log output. lgr: The base logger to augment. keysAndValues: Variadic list of key-value pairs to associate with the logger. Returns a pointer to the new logger with the added values.
Types ¶
type Options ¶
type Options struct {
// Level is the minimum log level (-1=Debug, 0=Info, 1=Warn, 2=Error).
Level int8
// Format is the output format (json or text).
Format LogFormat
// Timestamps controls whether timestamps are included in log output.
Timestamps bool
}
Options configures the logger behavior.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns the default logger options.