logger

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 14 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"
)
View Source
const (
	// LevelNone disables all structured log output.
	LevelNone = "none"
	// LevelError shows only error-level logs.
	LevelError = "error"
	// LevelWarn shows warning and error logs.
	LevelWarn = "warn"
	// LevelInfo shows info, warning, and error logs.
	LevelInfo = "info"
	// LevelDebug shows debug (V(1)) and above logs.
	LevelDebug = "debug"
	// LevelTrace shows trace (V(2)) and above logs.
	LevelTrace = "trace"
)

Named log level constants used in config and CLI flags.

View Source
const LogLevelNone = slog.Level(math.MaxInt32)

LogLevelNone is the sentinel slog.Level value that silences all log output. It is set to math.MaxInt32 so no log entry can reach it.

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 slog.Level) *logr.Logger

Get initializes the global logger with default options and the given slog level. 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 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 logr logger with custom options using a slog handler for local output and an otellogr sink for OTel export. It can only be called once. Subsequent calls will have no effect. This function must be called before using FromContext or any logging operations.

If telemetry.Setup has been called before this function, the otellogr sink will forward log records to the configured OTel LoggerProvider. Otherwise the noop provider is used, which is safe for unit tests.

func IsDebugLevel added in v0.2.0

func IsDebugLevel(level string) bool

IsDebugLevel returns true if the given log level string resolves to debug or deeper. This is used by the writer package to gate debug output.

func ParseLogLevel added in v0.2.0

func ParseLogLevel(s string) (slog.Level, error)

ParseLogLevel converts a named or numeric log level string to a slog.Level. Named levels: none, error, warn, info, debug, trace. Numeric levels: any integer string (e.g., "3", "5") is treated as a logr V-level and negated to produce the corresponding slog level (user's 3 → slog.Level(-3) → V(3) visible).

The logr/slog bridge maps logr V-level n to slog.Level(-n), so the handler threshold must also be -n to enable that level.

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"
	// FormatConsole outputs logs in human-readable text format.
	FormatConsole LogFormat = "console"
	// FormatText is an alias for FormatConsole for backward compatibility with config files.
	FormatText LogFormat = "text"
)

type Options

type Options struct {
	// Level is the minimum slog log level. Use ParseLogLevel to obtain from a string.
	Level slog.Level
	// Format is the output format (json, console, or text).
	Format LogFormat
	// Timestamps controls whether timestamps are included in log output.
	Timestamps bool
	// FilePath is an optional file path to write logs to. When set, logs are
	// written to this file. If empty, logs go to stderr.
	FilePath string
	// AlsoStderr controls whether logs are also written to stderr when FilePath
	// is set. When FilePath is empty, this has no effect (stderr is always used).
	AlsoStderr 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