logger v1
A simple, file-based logging system for Go applications with support for different log levels and trace IDs.
Repository
Features
Multiple log levels (INFO, ERROR, DEBUG)
File-based logging with daily rotation
Trace ID support for distributed systems
Asynchronous file writing
Graceful shutdown handling
Environment variable configuration
Log file management utilities
Installation
go get github.com/your-repo/logger
Usage
Basic Logging
import "github.com/your-repo/logger"
// Basic logging
logger.Info("Application started")
logger.Error("Something went wrong:", err)
logger.Debug("Debug information") // Only prints if LOG_DEBUG=true
Traced Logging (for distributed systems)
// Logging with trace IDs
traceID := "req-123"
logger.TracedInfo(traceID, "Request received")
logger.TracedError(traceID, "Request failed:", err)
logger.TracedDebug(traceID, "Processing request") // Only prints if LOG_DEBUG=true
Log File Management
// List all log files
files := logger.ListLogFiles()
// Read contents of a specific log file
content := logger.ReadLogFile("2024-03-20.log")
Configuration
Environment Variables
LOG_DEBUG
: Set totrue
to enable debug logging (default:false
)
Log Format
Regular logs:
[LEVEL] YYYY-MM-DD HH:mm:ss message
Traced logs:
[LEVEL] YYYY-MM-DD HH:mm:ss (trace: trace-id) message
Log File Location
Logs are stored in the logs
directory, with files named by date (YYYY-MM-DD.log)
Features Details
Log Levels
INFO
: General information messagesERROR
: Error messages and exceptionsDEBUG
: Detailed debug information (only when LOG_DEBUG=true)
File Management
Logs are automatically rotated daily
Files are created in the
logs
directoryEach log entry is appended with a newline
Buffered writing with periodic flush (every second)
Shutdown Handling
The package automatically handles:
System interrupts (SIGTERM)
Panic recovery
Graceful log flushing before exit
Implementation Notes
Log messages are temporarily stored in memory and flushed to disk every second
The package automatically creates the logs directory if it doesn't exist
All file operations use appropriate permissions (0644)
Thread-safe operations for concurrent logging
Best Practices
Use traced logging when tracking requests across distributed systems
Enable debug logging only when needed
Regularly monitor and rotate old log files
Handle the returned errors when reading log files
Last updated