It would make the most sense for this feature to utilise the existing logrotate
bash command. It should only be a matter of the existing website creation process / scripting setting up a logrotate.d
configuration file for each website upon website deployment. At the simplest, standard logrotate
settings could be used, rather than allowing the site admin or user to modify them.
Here's a summary of the log rotation routine that follows (which is a fairly standard routine):
Daily Rotation: The PHP error log file is rotated every day to manage log size and maintain organization of log entries over time.
Size-Based Rotation: Logs are rotated sooner if they exceed 5MB within the same day, ensuring that heavy logging does not lead to oversized files.
Retention Policy: The system retains unarchived daily logs for 7 days to allow quick access to recent log entries without compressing.
Archiving: Logs older than 7 days are moved to an archive directory where they are kept for an additional 30 days, ensuring that older logs remain available if needed for audits or analysis.
Compression: Archived logs are compressed to save disk space, with the most recent log remaining uncompressed for one rotation cycle to facilitate easy access.
Automated Deletion: Archived logs are automatically deleted after exceeding a 30-day retention period, allowing for efficient long-term log management without manual intervention.
Maintenance and Reloading: During rotation, the log file is truncated, and proper permissions are set for new log files. The logging service is refreshed to continue proper logging.
This intention of a setup like this is to balance the need for detailed operational records with disk space conservation, making it suitable for systems requiring efficient log management and quick retrieval of recent log data.
Here is a logrotate.d
configuration to achieve the above routine:
/var/www/SITE-ID/php-error.log {
daily # Rotate logs daily
rotate 37 # Keep logs for 7 days + 30 days of archived logs
size 5M # Rotate sooner if size exceeds 5MB
compress # Compress older logs to save space
delaycompress # Don't compress the log for the most recent rotation
missingok # If the log file is missing, do not issue an error
notifempty # Do not rotate if the log file is empty
create 640 root adm # Create a new log file with specified permissions
dateext # Append date to the rotated log file name
dateformat -%Y%m%d # Log file name format
sharedscripts # Run prerotate / postrotate scripts only once
postrotate
/usr/lib/rsyslog/rsyslog-rotate # Reload the log using the appropriate service — This needs to be adapted to the Enhance logging environment
endscript
# Archiving the 8th day log
olddir /var/www/SITE-ID/logs/ # Move 8th day log to archive directory
maxage 30 # Delete logs older than 30 days in archive
}
Notes
- The
/var/www/SITE-ID/log
directory would need to be created in advance as part of Enhance website deployment, so that it exists by the time logrotate
kicks in.
- Service Reload Command: The
postrotate
section needs to contain the correct command for the Enhance specific system to ensure the PHP error logging service reloads properly.