How to create log files with timestamps in filenames using Logrotate

Rename the Logrotate-managed log files to include timestamps in filenames.

Any Logrotate-managed log files can be include timestamps in the filenames, such as log-2023-01-01.log.

For this to work, update your logrotate configuration. In our example below, we're using Nginx log files.

  1. Edit the logrotate configuration file:

     $ nano /etc/logrotate.d/nginx
  2. Add a new directive to enforce timestamps into the Nginx log files filenames:

     /var/log/nginx/*.log {
         dateext
         dateformat -%y%m%d
         extension .log
         ...
     }

    If the directive already exists, simply add in:

     dateext
     dateformat -%y%m%d

    Logrotate with Nginx log file updated for timestamps

  3. Save the /etc/logrotate.d/nginx file changes.