How to Configure logrotate to Use Date-Based Naming for Rotated Log Files (access.log.YYYYMMDD.gz)


10 views

By default, logrotate uses sequential numbering for rotated log files (access.log.1.gz, access.log.2.gz). While this works, many system administrators prefer date-based naming for better chronological organization. This is particularly useful for:

  • Daily log analysis
  • Compliance requirements
  • Long-term log archiving
  • Easier correlation with system events

Here's how to configure logrotate to use date-based naming for lighttpd logs:

/var/log/lighttpd/access.log {
    daily
    missingok
    rotate 365
    compress
    delaycompress
    notifempty
    create 640 www-data adm
    sharedscripts
    dateext
    dateformat -%Y%m%d
    extension .gz
    postrotate
        /etc/init.d/lighttpd reload > /dev/null
    endscript
}

dateext: Enables date-based rotation instead of numbered rotation
dateformat: Specifies the date format (here: -%Y%m%d for YYYYMMDD)
extension: Appends .gz to rotated files (assuming compression)
rotate 365: Keeps logs for one year (adjust as needed)

For more complex scenarios, consider these additional parameters:

# Alternative date format including time:
dateformat -%Y%m%d-%H%M%S

# Monthly rotation with first-day naming:
monthly
dateformat -%Y%m01

# Keep logs for specific time periods:
maxage 90  # Delete logs older than 90 days
maxsize 100M  # Rotate when log reaches 100MB

Always test your configuration before deployment:

# Dry-run test:
logrotate -d /etc/logrotate.d/lighttpd

# Force immediate rotation:
logrotate -vf /etc/logrotate.d/lighttpd

For systems with high log volume, consider these optimizations:

  • Use copytruncate instead of create for continuous service
  • Adjust compression level with compresscmd and compressoptions
  • Implement log shipping before rotation with prerotate scripts

By default, logrotate uses incremental numbering for rotated log files (e.g., access.log.1.gz, access.log.2.gz). While this works for basic rotation, many system administrators prefer date-based naming for better log organization and historical tracking.

The simplest way to achieve date-based rotation is using the dateext directive in your logrotate configuration:

/var/log/lighttpd/access.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    dateext
    sharedscripts
    postrotate
        /etc/init.d/lighttpd reload > /dev/null
    endscript
}

For more precise control over the date format, you can combine dateext with dateformat:

/var/log/lighttpd/access.log {
    # ... other directives ...
    dateext
    dateformat -%Y%m%d
}

This will produce filenames like access.log-20231215.gz. Note the hyphen (-) prefix is required when specifying custom formats.

When rotating daily logs at midnight, you might want the rotated file to reflect the previous day's date:

/var/log/lighttpd/access.log {
    daily
    dateext
    dateyesterday
    dateformat -%Y%m%d
    # ... other directives ...
}

For comprehensive lighttpd log rotation, consider this complete configuration example:

/var/log/lighttpd/*.log {
    daily
    missingok
    rotate 365
    compress
    delaycompress
    notifempty
    dateext
    dateformat -%Y%m%d
    sharedscripts
    postrotate
        /usr/bin/killall -HUP lighttpd
    endscript
}

Always test your configuration before deployment:

logrotate -d /etc/logrotate.d/lighttpd

For a dry run that shows what would happen without actually rotating files:

logrotate -v /etc/logrotate.d/lighttpd