Documentation
¶
Overview ¶
Package logger provides structured logging for the xw application.
This package implements a simple but effective logging system with multiple log levels and optional debug mode. It provides:
- Multiple log levels (DEBUG, INFO, WARN, ERROR, FATAL)
- Structured output with timestamps and caller information
- Global and instance-based loggers
- Thread-safe operations
- Color-coded console output (when appropriate)
Example usage:
logger.Info("Server starting on port %d", 11581)
logger.Debug("Configuration loaded: %+v", cfg)
logger.Error("Failed to connect: %v", err)
Index ¶
- func Debug(format string, v ...interface{})
- func Error(format string, v ...interface{})
- func Fatal(format string, v ...interface{})
- func Info(format string, v ...interface{})
- func SetDebug(enable bool)
- func SetLevel(level Level)
- func Warn(format string, v ...interface{})
- type Level
- type Logger
- func (l *Logger) Debug(format string, v ...interface{})
- func (l *Logger) Error(format string, v ...interface{})
- func (l *Logger) Fatal(format string, v ...interface{})
- func (l *Logger) Info(format string, v ...interface{})
- func (l *Logger) Output(level Level, format string, v ...interface{})
- func (l *Logger) SetDebug(enable bool)
- func (l *Logger) SetLevel(level Level)
- func (l *Logger) Warn(format string, v ...interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(format string, v ...interface{})
Debug logs a debug message using the global logger
func Error ¶
func Error(format string, v ...interface{})
Error logs an error message using the global logger
func Fatal ¶
func Fatal(format string, v ...interface{})
Fatal logs a fatal error message and terminates the program
func Info ¶
func Info(format string, v ...interface{})
Info logs an informational message using the global logger
Types ¶
type Level ¶
type Level int
Level represents the severity level of a log message
const ( // DebugLevel is for detailed debugging information DebugLevel Level = iota // InfoLevel is for general informational messages InfoLevel // WarnLevel is for warning messages WarnLevel // ErrorLevel is for error messages ErrorLevel // FatalLevel is for fatal errors that cause program termination FatalLevel )
func ParseLevel ¶
ParseLevel converts a string to a log level.
Supported values: "debug", "info", "warn", "error", "fatal"
Returns InfoLevel if the string is not recognized.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger represents a logger instance with configurable output and level
func New ¶
New creates a new Logger instance.
Parameters:
- out: The output writer for log messages
- prefix: An optional prefix for all log messages
- flags: Log flags (see log package constants)
Returns:
- A pointer to a configured Logger
Example:
logger := logger.New(os.Stdout, "[xw] ", log.LstdFlags|log.Lshortfile)
func (*Logger) Debug ¶
Debug logs a debug message (only if debug mode is enabled).
Example:
logger.Debug("Processing request: %+v", req)
func (*Logger) Error ¶
Error logs an error message.
Example:
logger.Error("Failed to connect to database: %v", err)
func (*Logger) Fatal ¶
Fatal logs a fatal error message and terminates the program.
This function does not return.
Example:
logger.Fatal("Cannot start server: %v", err)
func (*Logger) Info ¶
Info logs an informational message.
Example:
logger.Info("Server started on port %d", 11581)
func (*Logger) Output ¶
Output writes a log message at the specified level.
This is the core logging function used by all level-specific functions.
func (*Logger) SetDebug ¶
SetDebug enables or disables debug logging.
When enabled, DEBUG level messages are printed.