LoggerFile Rotation
Automatic Cleanup
The logger automatically removes old rotated log files to prevent uncontrolled disk usage.
Cleanup behavior is controlled through the MaxLogFiles configuration option, which defines the maximum number of
managed log files kept on disk.
Example configuration:
package main
import (
"log/slog"
"github.com/netlifeguru/logger"
)
func main() {
closer, err := logger.Init(logger.Config{
Dir: "./logs",
MaxLogFiles: 5,
})
if err != nil {
slog.Error(err.Error())
}
defer closer.Close()
slog.Info("logger initialized")
}Example directory before cleanup:
logs/
├── 2026-05-08-0001.log
├── 2026-05-09-0001.log
├── 2026-05-10-0001.log
├── 2026-05-11-0001.log
├── 2026-05-11-0002.log
├── 2026-05-11-0003.log
└── 2026-05-11-0004.logAfter cleanup:
logs/
├── 2026-05-10-0001.log
├── 2026-05-11-0001.log
├── 2026-05-11-0002.log
├── 2026-05-11-0003.log
└── 2026-05-11-0004.logCleanup runs automatically during log rotation and removes the oldest managed log files first.
This helps:
- reduce disk usage
- simplify maintenance
- avoid oversized log directories
- keep deployments predictable over time
Default value:
MaxLogFiles: 5