A comprehensive, easy-to-use logging package for Go applications with colored output, multiple log levels, and flexible configuration options.
- π Colored Output: Beautiful colored console output for different log levels
- π Multiple Log Levels: VERBOSE, DEBUG, INFO, SUCCESS, WARNING, ERROR, FATAL
- βοΈ Configurable: Customizable prefixes, timestamps, colors, and log levels
- π Easy to Use: Simple API with both global and instance-based usage
- π¦ Zero Dependencies: No external dependencies required
- π― Production Ready: Includes production-friendly logger configurations
To install this package, use go get:
go get github.com/hoanle396/loggerThen import it in your Go files:
import "github.com/hoanle396/logger"package main
import "github.com/hoanle396/logger"
func main() {
logger.Info("Application started")
logger.Success("Operation completed successfully")
logger.Warning("This is a warning")
logger.Error("An error occurred")
logger.Debug("Debug information")
logger.Verbose("Verbose details")
}package main
import "github.com/hoanle396/logger"
func main() {
// Create a custom logger
myLogger := logger.NewLogger()
myLogger.SetPrefix("MYAPP")
myLogger.SetLevel(logger.DEBUG)
myLogger.Info("Custom logger message")
myLogger.Debug("Debug with custom logger")
}The package supports the following log levels (in order of priority):
| Level | Description | Color | Use Case |
|---|---|---|---|
VERBOSE |
Detailed tracing information | Gray | Development debugging |
DEBUG |
Debug information | Cyan | Development |
INFO |
General information | Blue | General app flow |
SUCCESS |
Success operations | Green | Successful operations |
WARNING |
Warning conditions | Yellow | Potential issues |
ERROR |
Error conditions | Red | Errors that don't stop execution |
FATAL |
Fatal errors | Red | Critical errors (exits program) |
These functions use the default global logger instance:
logger.Verbose(message string, args ...interface{})
logger.Debug(message string, args ...interface{})
logger.Info(message string, args ...interface{})
logger.Success(message string, args ...interface{})
logger.Warning(message string, args ...interface{})
logger.Error(message string, args ...interface{})
logger.Fatal(message string, args ...interface{})Configure the default logger:
logger.SetLevel(logger.DEBUG) // Set minimum log level
logger.SetShowTime(false) // Disable timestamps
logger.SetColorized(false) // Disable colors
logger.SetPrefix("MYAPP") // Set prefix for all messages// Standard logger with default settings
logger := logger.NewLogger()logger.SetLevel(level LogLevel) // Set minimum log level
logger.SetShowTime(show bool) // Enable/disable timestamps
logger.SetColorized(colorized bool) // Enable/disable colors
logger.SetPrefix(prefix string) // Set message prefixlogger.Verbose(message string, args ...interface{})
logger.Debug(message string, args ...interface{})
logger.Info(message string, args ...interface{})
logger.Success(message string, args ...interface{})
logger.Warning(message string, args ...interface{})
logger.Error(message string, args ...interface{})
logger.Fatal(message string, args ...interface{})logger.Info("Server starting on port %d", 8080)
logger.Success("Database connection established")
logger.Warning("Memory usage is at %d%%", 85)
logger.Error("Failed to process request: %v", err)// Set to WARNING level - only WARNING, ERROR, and FATAL will show
logger.SetLevel(logger.WARNING)
logger.Debug("This won't show") // Hidden
logger.Info("This won't show") // Hidden
logger.Warning("This will show") // Visible
logger.Error("This will show") // Visible// Database logger
dbLogger := logger.NewLogger()
dbLogger.SetPrefix("DB")
dbLogger.Info("Connection pool initialized")
// HTTP logger
httpLogger := logger.NewLogger()
httpLogger.SetPrefix("HTTP")
httpLogger.Info("Server listening on :8080")
// Auth logger
authLogger := logger.NewLogger()
authLogger.SetPrefix("AUTH")
authLogger.Warning("Failed login attempt from %s", clientIP)// Disable colors when running in CI/CD environments
if os.Getenv("CI") != "" {
logger.SetColorized(false)
}func main() {
appLogger := logger.NewLogger()
appLogger.SetPrefix("MYAPP")
appLogger.Info("Application starting...")
appLogger.Debug("Loading configuration...")
if err := loadConfig(); err != nil {
appLogger.Fatal("Failed to load configuration: %v", err)
}
appLogger.Success("Configuration loaded")
appLogger.Debug("Connecting to database...")
if err := connectDB(); err != nil {
appLogger.Error("Database connection failed: %v", err)
appLogger.Fatal("Cannot start without database")
}
appLogger.Success("Database connected")
appLogger.Info("Application ready")
}When colors are enabled, each log level has its distinct color:
- VERBOSE: Gray
- DEBUG: Cyan
- INFO: Blue
- SUCCESS: Green
- WARNING: Yellow
- ERROR: Red
- FATAL: Red
Colors automatically reset after each message to prevent affecting other terminal output.
The default log format is:
[TIMESTAMP] [PREFIX] [LEVEL] MESSAGE
Example output:
[2024-01-15 14:30:25] [APP] [INFO] Server starting on port 8080
[2024-01-15 14:30:25] [APP] [SUCCESS] Database connection established
[2024-01-15 14:30:26] [APP] [WARNING] High memory usage detected: 85%
[2024-01-15 14:30:27] [APP] [ERROR] Failed to send notification email
- Use appropriate log levels: Don't use ERROR for non-error conditions
- Set proper log levels for environments: VERBOSE/DEBUG for development, INFO+ for production
- Use prefixes for different components: Makes it easier to filter logs
- Disable colors in production: Better for log aggregation systems
- Use Fatal sparingly: Only for conditions that require immediate program termination
- Format messages properly: Use format strings for dynamic content
The logger is safe for concurrent use. Multiple goroutines can call logging methods simultaneously without additional synchronization.
The logger is designed to be fast and efficient:
- Log level checking happens before message formatting
- Color codes are applied only when needed
- Minimal memory allocations for formatted messages
This logger package is provided as-is for educational and development purposes.