Documentation
¶
Index ¶
- func Debug(msg string, attrs ...any)
- func Debugf(format string, args ...any)
- func Error(msg string, attrs ...any)
- func Errorf(format string, args ...any)
- func Info(msg string, attrs ...any)
- func Infof(format string, args ...any)
- func Init(config *Config, attrs map[string]any)
- func NewSlogLogger(config *Config, options ...Option) *slog.Logger
- func Sync() error
- func Warn(msg string, attrs ...any)
- func Warnf(format string, args ...any)
- type AddCallerStatus
- type BaseLogger
- type Config
- type ConsoleConfig
- type Field
- func Any(key string, value interface{}) Field
- func Bool(key string, value bool) Field
- func Duration(key string, value time.Duration) Field
- func Err(err error) Field
- func Float64(key string, value float64) Field
- func Group(key string, values ...any) Field
- func Int(key string, value int) Field
- func Int64(key string, value int64) Field
- func String(key, value string) Field
- func Time(key string, value time.Time) Field
- func Uint64(key string, value uint64) Field
- type FileConfig
- type Logger
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
Debug logs a debug-level message with optional structured attributes using the global logger.
func Debugf ¶
Debugf logs a debug-level message using printf-style formatting with the global logger.
func Error ¶
Error logs an error-level message with optional structured attributes using the global logger.
func Errorf ¶
Errorf logs an error-level message using printf-style formatting with the global logger.
func Info ¶
Info logs an info-level message with optional structured attributes using the global logger.
func Init ¶
Init initializes the global logger with the provided configuration and attributes. The configuration determines output destinations and formatting, while attrs are added to all log messages. This should be called once during application startup.
func NewSlogLogger ¶
NewSlogLogger creates a new structured logger using the standard library's slog interface. This provides compatibility with Go's standard structured logging while using zap as the backend.
func Sync ¶
func Sync() error
Sync flushes any buffered log entries to their destinations. This should typically be called before application shutdown to ensure all logs are written.
Types ¶
type AddCallerStatus ¶
type AddCallerStatus int
AddCallerStatus represents the state of caller information in log entries.
const ( // AddCallerStatusKeep maintains the current caller setting without changes. AddCallerStatusKeep AddCallerStatus = iota // AddCallerStatusEnable adds caller information to log entries. AddCallerStatusEnable // AddCallerStatusDisable removes caller information from log entries. AddCallerStatusDisable )
type BaseLogger ¶
type BaseLogger interface {
// Debug logs a debug-level message with optional structured attributes.
Debug(msg string, args ...any)
// Info logs an info-level message with optional structured attributes.
Info(msg string, args ...any)
// Warn logs a warning-level message with optional structured attributes.
Warn(msg string, args ...any)
// Error logs an error-level message with optional structured attributes.
Error(msg string, args ...any)
}
BaseLogger defines the core logging interface with structured logging capabilities. It provides the four standard log levels with support for structured attributes.
type Config ¶
type Config struct {
File *FileConfig `json:"file" yaml:"file"`
Console *ConsoleConfig `json:"console" yaml:"console"`
Level string `json:"level" yaml:"level"`
}
Config defines the complete logging configuration including output destinations, log level, and formatting options. It supports both console and file outputs.
func NewDefaultConfig ¶
func NewDefaultConfig() *Config
NewDefaultConfig creates a new Config with sensible defaults. Returns a configuration with info level logging and no file output.
type ConsoleConfig ¶
type ConsoleConfig struct {
Disable bool `json:"disable" yaml:"disable"`
}
ConsoleConfig configures console logging output. When Disable is true, console logging is completely turned off.
type Field ¶
Field is an alias for zap.Field providing structured logging fields.
func Any ¶
Any creates a structured field with an arbitrary value. The value will be serialized using the best available method.
type FileConfig ¶
type FileConfig struct {
FileName string `json:"file_name" yaml:"file_name"`
MaxSize int `json:"max_size" yaml:"max_size"`
MaxBackups int `json:"max_backups" yaml:"max_backups"`
MaxAge int `json:"max_age" yaml:"max_age"`
}
FileConfig configures file-based logging output with rotation settings. It supports automatic log rotation based on size, age, and backup count.
type Logger ¶
type Logger interface {
BaseLogger
// Debugf logs a debug-level message using printf-style formatting.
Debugf(format string, args ...any)
// Infof logs an info-level message using printf-style formatting.
Infof(format string, args ...any)
// Warnf logs a warning-level message using printf-style formatting.
Warnf(format string, args ...any)
// Errorf logs an error-level message using printf-style formatting.
Errorf(format string, args ...any)
}
Logger extends BaseLogger with formatted logging capabilities. It combines structured logging with traditional printf-style formatting.
type Option ¶
type Option = func(*options)
Option is a function type for configuring logger options.
func AddCaller ¶
func AddCaller() Option
AddCaller enables caller information in log entries. This includes file names and line numbers where the log call was made.
func AddCallerSkip ¶
AddCallerSkip adjusts the caller skip count for accurate call site reporting. This is useful when wrapping the logger to ensure the correct caller is reported.
func DisableCaller ¶
func DisableCaller() Option
DisableCaller removes caller information from log entries. This can improve performance when caller information is not needed.
func WithAttrs ¶
WithAttrs adds structured attributes to all log messages from this logger. These attributes provide consistent context across all log entries.
func WithName ¶
WithName sets the logger name for identification purposes. The name appears in log output to help distinguish between different loggers.
func WithStackAt ¶
WithStackAt enables stack trace logging at the specified level and above. Stack traces help debug issues by showing the full call chain.