Documentation
¶
Overview ¶
Package logging provides structured logging utilities for reputer.
Three logging modes: CLI (user-friendly), text (debug), JSON (structured). All modes write to stderr and use log/slog under the hood.
Index ¶
- Constants
- func NewCLILogger(level string) *slog.Logger
- func NewLogLogger(module, version, level string) *log.Logger
- func NewStructuredLogger(module, version, level string) *slog.Logger
- func NewTextLogger(module, version, level string) *slog.Logger
- func ParseLogLevel(level string) slog.Level
- func SetDefaultCLILogger(level string)
- func SetDefaultLogger(module, version string)
- func SetDefaultLoggerWithLevel(module, version, level string)
- func SetDefaultStructuredLogger(module, version string)
- func SetDefaultStructuredLoggerWithLevel(module, version, level string)
- type CLIHandler
Constants ¶
const (
// EnvVarLogLevel is the environment variable name for setting the log level.
EnvVarLogLevel = "LOG_LEVEL"
)
const LogPrefixEnvVar = "REPUTER_LOG_PREFIX"
LogPrefixEnvVar is the environment variable name for customizing the log prefix.
Variables ¶
This section is empty.
Functions ¶
func NewCLILogger ¶
NewCLILogger creates a new logger with CLI-friendly output format. This logger outputs minimal, user-friendly messages: - Normal messages: just the message text - Error messages: message text in red color Parameters:
- level: The log level as a string (e.g., "debug", "info", "warn", "error").
Returns:
- *slog.Logger: A pointer to the configured slog.Logger instance with CLI handler.
func NewLogLogger ¶
NewLogLogger creates a new standard library log.Logger that writes logs using the slog package with the specified log level. Parameters:
- level: The log level as a slog.Level.
Returns:
- *log.Logger: A pointer to the configured log.Logger instance.
func NewStructuredLogger ¶
NewStructuredLogger creates a new structured logger with the specified log level Defined module name and version are included in the logger's context. AddSource is enabled for debug level logging only. Parameters:
- module: The name of the module/application using the logger.
- version: The version of the module/application (e.g., "v1.0.0").
- level: The log level as a string (e.g., "debug", "info", "warn", "error").
Returns:
- *slog.Logger: A pointer to the configured slog.Logger instance.
func NewTextLogger ¶
NewTextLogger creates a new text logger with the specified log level Defined module name and version are included in the logger's context. AddSource is enabled for debug level logging only. Parameters:
- module: The name of the module/application using the logger.
- version: The version of the module/application (e.g., "v1.0.0").
- level: The log level as a string (e.g., "debug", "info", "warn", "error").
Returns:
- *slog.Logger: A pointer to the configured slog.Logger instance with text handler.
func ParseLogLevel ¶
ParseLogLevel converts a string representation of a log level into a slog.Level. Parameters:
- level: The log level as a string (e.g., "debug", "info", "warn", "error").
Returns:
- slog.Level corresponding to the input string. Defaults to slog.LevelInfo for unrecognized strings.
func SetDefaultCLILogger ¶
func SetDefaultCLILogger(level string)
SetDefaultCLILogger initializes the CLI logger with the appropriate log level and sets it as the default logger. Parameters:
- level: The log level as a string (e.g., "debug", "info", "warn", "error").
func SetDefaultLogger ¶
func SetDefaultLogger(module, version string)
SetDefaultLogger initializes the text logger with the appropriate log level and sets it as the default logger. Defined module name and version are included in the logger's context. Parameters:
- module: The name of the module/application using the logger.
- version: The version of the module/application (e.g., "v1.0.0").
Derives log level from the LOG_LEVEL environment variable.
func SetDefaultLoggerWithLevel ¶
func SetDefaultLoggerWithLevel(module, version, level string)
SetDefaultLoggerWithLevel initializes the text logger with the specified log level and sets it as the default logger. Defined module name and version are included in the logger's context. Parameters:
- module: The name of the module/application using the logger.
- version: The version of the module/application (e.g., "v1.0.0").
- level: The log level as a string (e.g., "debug", "info", "warn", "error").
func SetDefaultStructuredLogger ¶
func SetDefaultStructuredLogger(module, version string)
SetDefaultStructuredLogger initializes the structured logger with the appropriate log level and sets it as the default logger. Defined module name and version are included in the logger's context. Parameters:
- module: The name of the module/application using the logger.
- version: The version of the module/application (e.g., "v1.0.0").
Derives log level from the LOG_LEVEL environment variable.
func SetDefaultStructuredLoggerWithLevel ¶
func SetDefaultStructuredLoggerWithLevel(module, version, level string)
SetDefaultStructuredLoggerWithLevel initializes the structured logger with the specified log level Defined module name and version are included in the logger's context. Parameters:
- module: The name of the module/application using the logger.
- version: The version of the module/application (e.g., "v1.0.0").
- level: The log level as a string (e.g., "debug", "info", "warn", "error").
Types ¶
type CLIHandler ¶
type CLIHandler struct {
// contains filtered or unexported fields
}
CLIHandler is a custom slog.Handler for CLI output. It formats log messages in a user-friendly way: - Non-error messages: just the message text - Error messages: message text in red
func NewCLIHandler ¶
func NewCLIHandler(w io.Writer, level slog.Level) *CLIHandler
NewCLIHandler creates a new CLI handler that writes to the given writer.
func (*CLIHandler) Enabled ¶
Enabled returns true if the handler handles records at the given level.