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 IsDebugLevel(level string) bool
- func ParseLogLevel(s string) (zapcore.Level, error)
- 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" )
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 = zapcore.FatalLevel + 1
LogLevelNone is the sentinel zap level value that silences all log output. It is set above FatalLevel 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 Zap and Logr loggers with default options. It can only be called once. Subsequent calls will have no effect. logLevel: The minimum zap logging level (e.g., -1=Debug/V(1), 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 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 zapcore.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 zap level (user's 3 → zap -3 → V(3) visible).
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 zap log level.
Level zapcore.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.