How to Configure Log Rotation in Go

Configure log rotation for Go applications using external tools like logrotate or internal libraries like lumberjack to manage file size and history.

Go does not have a built-in log rotation mechanism; you must configure it using an external tool like logrotate or implement it in your code using the golang.org/x/exp/slog or lumberjack packages. For a standard production setup, configure logrotate to manage your Go application's log files by creating a configuration file at /etc/logrotate.d/your-app with the following content:

/var/log/your-app/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 your-user your-group
    sharedscripts
    postrotate
        /usr/bin/killall -HUP your-app
    endscript
}

This setup rotates logs daily, keeps 14 days of history, compresses old logs, and sends a HUP signal to your Go application to reopen log files.