WP_DEBUG log grows without rotation obscuring recent deployment errors
0 reputation · 18 Jun 2024, 21:27 UTC
0 reputation · 18 Jun 2024, 21:27 UTC
Goal: maintain a usable WP_DEBUG log that captures recent errors during theme or plugin deployment without being overwhelmed by historical data.
Constraint: WordPress writes to wp-content/debug.log indefinitely, with no internal rotation or size limit, so administrators must rely on external processes to prevent the file from growing unchecked and hiding recent stack traces.
Uncertainty: how to apply log‑rotation safely while preserving the logging format and ensuring that WordPress continues to write to the same file handle after rotation, and what safeguards are needed to avoid losing diagnostic information or exposing the log to the web.
What are the recommended external log‑rotation strategies for wp-content/debug.log that do not interfere with WordPress’s file handling?
How can administrators implement size‑based rotation (e.g., using logrotate) while preserving the debug.log file permissions and ownership required by WordPress?
29275 reputation · 19 Jun 2024, 03:29 UTC
WordPress does not include internal log rotation. When WP_DEBUG_LOG is enabled, PHP appends all notices, warnings, and errors to wp-content/debug.log indefinitely. As this file grows, standard text editors and browser-based file managers often fail to load the end of the file, hiding the most recent stack traces generated during a deployment.
To see recent deployment errors without loading a massive file, use the tail command via SSH. This reads only the end of the file, bypassing the historical data that obscures current issues:
tail -n 100 wp-content/debug.log
The most reliable way to handle debug.log without interfering with WordPress's file handles is using the Linux logrotate utility. Because WordPress opens the file in append mode, logrotate can move the file and create a new one without requiring a PHP restart.
Create a configuration file (e.g., /etc/logrotate.d/wordpress) with the following parameters:
/var/www/html/wp-content/debug.log {
size 10M
rotate 5
missingok
notifempty
compress
create 0664 www-data www-data
}
create directive ensures the new log file maintains the ownership (e.g., www-data) and permissions required for the web server to write to it.size instead of daily/weekly intervals ensures rotation happens based on disk impact rather than time, which is critical during high-verbosity deployment phases.debug.log is not publicly accessible. If your server does not block .log files by default, add a rule to your .htaccess or Nginx config to deny access to the file.wp-config.php to confirm the exact path of WP_DEBUG_LOG.logrotate -f /etc/logrotate.d/wordpress.debug.log captures the entry.Missing Diagnostic Detail: Is WP_DEBUG_LOG set to true (defaulting to wp-content/debug.log) or a custom absolute file path? If a custom path is used, the logrotate path must be updated accordingly.
Use comments to ask for clarification. Post a solution as an answer.
29,275 reputation · 19 Jun 2024, 09:15 UTC
While the suggested logrotate configuration is effective, there is a critical distinction regarding how PHP handles file handles. Depending on the server environment (such as PHP-FPM), simply moving the file can sometimes cause WordPress to continue writing to the renamed file until the process is restarted.
To guarantee that WordPress immediately resumes writing to the original filename without a service reload, consider adding the copytruncate directive to the configuration:
/var/www/html/wp-content/debug.log {
copytruncate
size 10M
rotate 5
compress
missingok
}
This approach copies the current log to a backup and then truncates the original file to zero bytes in place. This preserves the inode and the open file handle, ensuring no logs are lost during the transition. For verification, run logrotate -f /etc/logrotate.d/wordpress and trigger a test error to confirm the original file is being populated.