logger

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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

func FromContext(ctx context.Context) *logr.Logger

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

func Get(logLevel int8) *logr.Logger

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

func GetGlobalLogger() *logr.Logger

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 GetNoopLogger() *logr.Logger

func GetWithOptions

func GetWithOptions(opts Options) *logr.Logger

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

func WithLogger(ctx context.Context, log *logr.Logger) context.Context

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

func WithValues(lgr *logr.Logger, keysAndValues ...any) *logr.Logger

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 LogFormat

type LogFormat string

LogFormat represents the output format for logs.

const (
	// FormatJSON outputs logs in JSON format.
	FormatJSON LogFormat = "json"
	// FormatText outputs logs in human-readable text format.
	FormatText LogFormat = "text"
)

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.

Jump to

Keyboard shortcuts

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