Documentation
¶
Overview ¶
Package logx provides a unified logging interface with support for multiple backends. It supports both zap and slog loggers with features like log rotation, buffering, and performance optimization.
Example usage:
config := &logx.LoggerConfig{
Type: logx.LoggerTypeZap,
Level: "info",
LogInConsole: true,
Dir: "logs",
Format: "json",
}
err := logx.Init(config)
if err != nil {
log.Fatal(err)
}
logx.Info("Hello, world!")
Index ¶
- func Debug(args ...any)
- func Debugf(template string, args ...any)
- func Error(args ...any)
- func Errorf(template string, args ...any)
- func Fatal(args ...any)
- func Fatalf(template string, args ...any)
- func Info(args ...any)
- func Infof(template string, args ...any)
- func Init(c *LoggerConfig) error
- func Warn(args ...any)
- func Warnf(template string, args ...any)
- type LogFormat
- type Logger
- type LoggerConfig
- type LoggerType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(args ...any)
Debug logs a debug message using the global logger. If the global logger is not initialized, this function does nothing.
func Error ¶
func Error(args ...any)
Error logs an error message using the global logger. If the global logger is not initialized, this function does nothing.
func Fatal ¶
func Fatal(args ...any)
Fatal logs a fatal message using the global logger and exits the program. If the global logger is not initialized, this function does nothing.
func Fatalf ¶ added in v1.1.0
Fatalf logs a formatted fatal message using the global logger and exits the program.
func Info ¶
func Info(args ...any)
Info logs an informational message using the global logger. If the global logger is not initialized, this function does nothing.
func Init ¶
func Init(c *LoggerConfig) error
Init initializes the logger according to the configuration. It returns an error if the configuration is invalid or initialization fails.
Types ¶
type Logger ¶
type Logger interface {
// Debug logs a debug message.
Debug(args ...any)
// Debugf logs a formatted debug message.
Debugf(template string, args ...any)
// Info logs an informational message.
Info(args ...any)
// Infof logs a formatted informational message.
Infof(template string, args ...any)
// Warn logs a warning message.
Warn(args ...any)
// Warnf logs a formatted warning message.
Warnf(template string, args ...any)
// Error logs an error message.
Error(args ...any)
// Errorf logs a formatted error message.
Errorf(template string, args ...any)
// Fatal logs a fatal message and exits the program.
Fatal(args ...any)
// Fatalf logs a formatted fatal message and exits the program.
Fatalf(template string, args ...any)
}
Logger defines the interface for logging operations. All logger implementations must satisfy this interface.
type LoggerConfig ¶
type LoggerConfig struct {
// Type specifies the logger backend type (slog or zap).
Type LoggerType `mapstructure:"type" yaml:"type"`
// Level specifies the minimum log level (debug, info, warn, error, fatal).
Level string `mapstructure:"level" yaml:"level"`
// LogInConsole determines whether to output logs to console.
LogInConsole bool `mapstructure:"log-in-console" yaml:"log-in-console"`
// Dir specifies the directory where log files are stored.
Dir string `mapstructure:"dir" yaml:"dir"`
// Format specifies the log output format (text or json).
Format string `mapstructure:"format" yaml:"format"`
// MaxAge specifies the maximum number of days to retain log files (0 means no limit).
MaxAge int `mapstructure:"max-age" yaml:"max-age"`
// MaxSize specifies the maximum size of a log file in MB (0 means no limit).
MaxSize int `mapstructure:"max-size" yaml:"max-size"`
// MaxBackups specifies the maximum number of log files to keep (0 means no limit).
MaxBackups int `mapstructure:"max-backups" yaml:"max-backups"`
// LocalTime determines whether to use local time (true) or UTC time (false).
LocalTime bool `mapstructure:"local-time" yaml:"local-time"`
// Compress determines whether to compress rotated log files.
Compress bool `mapstructure:"compress" yaml:"compress"`
// BufferSize specifies the buffer size for performance optimization (0 disables buffering).
BufferSize int `mapstructure:"buffer-size" yaml:"buffer-size"`
// FlushInterval specifies the interval in seconds to flush the buffer.
FlushInterval int `mapstructure:"flush-interval" yaml:"flush-interval"`
}
LoggerConfig holds the configuration for the logger.
type LoggerType ¶
type LoggerType string
LoggerType represents the type of logger backend.
const ( // LoggerTypeSlog uses Go's standard slog logger. LoggerTypeSlog LoggerType = "slog" // LoggerTypeZap uses Uber's zap logger for high performance. LoggerTypeZap LoggerType = "zap" )
Directories
¶
| Path | Synopsis |
|---|---|
|
Package slog provides a slog logger implementation for the logx package.
|
Package slog provides a slog logger implementation for the logx package. |
|
Package zap provides a zap logger implementation for the logx package.
|
Package zap provides a zap logger implementation for the logx package. |