Documentation
¶
Overview ¶
Package logutil provides general logging utilities for log/slog integration.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewLogFromSlog ¶
NewLogFromSlog creates a standard log.Logger that writes to the provided slog.Logger.
func ValidFormat ¶
ValidFormat returns true if the log format is valid.
func ValidLevel ¶
ValidLevel returns true if the log level is valid.
Types ¶
type Config ¶
type Config struct {
Out io.Writer
Format LogFormat
Level LogLevel
CommonAttr []Attr
HookFn HookFunc
}
Config holds common logger parameters.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config instance with default settings.
func (*Config) SlogDefaultLogger ¶
SlogDefaultLogger set and return a slog logger based on the Config settings.
func (*Config) SlogHandler ¶
SlogHandler returns a new slog Handler based on the Config settings.
func (*Config) SlogLogger ¶
SlogLogger returns a slog logger based on the Config settings.
type HookFunc ¶
HookFunc is used to intercept the log message before passing it to the underlying handler.
type LogFormat ¶
type LogFormat int8
LogFormat represents the logging output format.
func ParseFormat ¶
ParseFormat converts a string to a log format.
type LogLevel ¶
LogLevel is an alias for slog.Level to represent extended log severity levels.
const ( LevelEmergency LogLevel = 64 // (+) 0 - Emergency - System is unusable. LevelAlert LogLevel = 32 // (+) 1 - Alert - Immediate action required. LevelCritical LogLevel = 16 // (+) 2 - Critical - Critical conditions. LevelError LogLevel = 8 // (=) 3 - Error - Error conditions. LevelWarning LogLevel = 4 // (=) 4 - Warning - Warning conditions. LevelNotice LogLevel = 2 // (+) 5 - Notice - Normal but noteworthy events. LevelInfo LogLevel = 0 // (=) 6 - Informational - General informational messages. LevelDebug LogLevel = -4 // (=) 7 - Debug - Detailed debugging information. LevelTrace LogLevel = -8 // (+) Additional Trace level when supported. )
Extended slog levels.
func ParseLevel ¶
ParseLevel converts syslog standard level strings to log/slog levels. Syslog uses eight severity levels, ranging from 0 (Emergency) to 7 (Debug). The lower the number, the higher the priority.
type Option ¶
Option is a type alias for a function that updates the Config.
func WithCommonAttr ¶
WithCommonAttr adds common attributes to the logger.
func WithHookFn ¶
WithHookFn adds a function to intercept the log message before passing it to the underlying handler.
func WithOutWriter ¶
WithOutWriter overrides the output io.Writer.
type SlogHookHandler ¶
SlogHookHandler is a slog.Handler that wraps another handler to add custom logic.
func NewSlogHookHandler ¶
func NewSlogHookHandler(h slog.Handler, f HookFunc) *SlogHookHandler
NewSlogHookHandler adds a hook function to the slog Handler.
type SlogWriter ¶
SlogWriter is a custom io.Writer that writes to slog.Logger at the error level.
func NewSlogWriter ¶
func NewSlogWriter(logger *slog.Logger) *SlogWriter
NewSlogWriter creates a new SlogWriter with the provided slog.Logger.