Documentation
¶
Index ¶
- Constants
- func FromContext(ctx context.Context) *logr.Logger
- func Get(logLevel slog.Level) *logr.Logger
- func GetGlobalLogger() *logr.Logger
- func GetNoopLogger() *logr.Logger
- func GetWithOptions(opts Options) *logr.Logger
- func IsDebugLevel(level string) bool
- func ParseLogLevel(s string) (slog.Level, error)
- 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" )
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.
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 ¶
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 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 ¶
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 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
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
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 ¶
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 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.