How to Enable Error Logging in WordPress

Contents

Introduction

Error logging is an indispensable tool for any WordPress developer or administrator seeking to diagnose, troubleshoot and maintain a stable website. By capturing PHP errors, warnings and notices, you gain visibility into malfunctioning code, deprecated functions, plugin conflicts and performance bottlenecks.

Why Enable Error Logging

  • Silent Failures: PHP errors often occur without visible symptoms logs reveal hidden issues.
  • Security: Instead of displaying errors to visitors, logs keep sensitive details private.
  • Performance: Detect and address resource-intensive warnings.
  • Quality Assurance: Monitor deprecated functions before major upgrades.

Prerequisites

  • Access to your WordPress installation files (via FTP/SFTP or hosting file manager).
  • A code editor to modify wp-config.php or php.ini.
  • Optional: Access to server logs (e.g. via cPanel or SSH).

Core Method: Using WP_DEBUG Constants

1. Open wp-config.php

Locate the wp-config.php file in your WordPress root directory.

2. Enable Debugging

// Enable WP debugging
define(WP_DEBUG, true)

// Log errors to wp-content/debug.log
define(WP_DEBUG_LOG, true)

// Prevent errors from showing on screen (production)
define(WP_DEBUG_DISPLAY, false)

// Optional: Force script concatenation off
define(SCRIPT_DEBUG, true)
  

After saving changes, WordPress will create or append to wp-content/debug.log.

3. Location of the Log File

The debug.log file resides in wp-content/. Ensure it is writable (chmod 660 or 644 depending on your server).

Alternative Methods

Method Configuration File Pros Cons
PHP.ini php.ini Logs all PHP errors server-wide. Requires server access affects all sites.
.htaccess .htaccess Easy if php.ini is inaccessible. May not be supported on all hosts.
WordPress Plugin N/A UI-driven, no code edits. Potential overhead plugin dependency.

Enabling via php.ini

error_reporting = E_ALL
log_errors = On
error_log = /path/to/your/site/php-error.log
display_errors = Off
  

Restart your web server after edits. Check /path/to/your/site/php-error.log.

Enabling via .htaccess

php_flag log_errors On
php_value error_log /home/username/public_html/php-error.log
php_value error_reporting 30711
  

Using Debugging Plugins

Server-Level Error Logs

Many hosts provide a “Logs” section in cPanel or Plesk. You can view:

  • Apache/Nginx error.log
  • PHP-FPM logs
  • Access logs for HTTP status codes

Connect via SSH and tail logs in real time:

tail -f /var/log/apache2/error.log
tail -f /var/log/php-fpm/www-error.log
  

Customizing PHP Error Levels

Fine-tune which errors are logged:

// In wp-config.php or plugin/bootstrap file
error_reporting(E_ALL  ~E_DEPRECATED  ~E_STRICT)
ini_set(log_errors, On)
ini_set(error_log, dirname(__FILE__) . /wp-content/custom-errors.log)
  

Analyzing and Managing Log Files

  • Rotation: Prevent files from growing indefinitely. Use logrotate on Linux:
/path/to/wp-content/debug.log {
  daily
  rotate 7
  compress
  missingok
  notifempty
  create 640 www-data www-data
}
  
  • Monitoring: Integrate with services like Sentry for real-time alerts.
  • Security: Ensure debug files are not publicly accessible. Deny via .htaccess:

  Require all denied

  

Best Practices and Security Considerations

  • Never leave WP_DEBUG_DISPLAY enabled on production.
  • Regularly review and purge old logs.
  • Restrict file permissions and directory access.
  • Document and track recurring errors in your issue tracker.

Troubleshooting Common Issues

  • No debug.log generated Verify WP_DEBUG and WP_DEBUG_LOG are true
    and file permissions allow writing.
  • Errors still displayed Ensure WP_DEBUG_DISPLAY is set to false.
  • php.ini overrides Some hosts disallow local php.ini use .htaccess or plugin methods.

Further Reading

By following these comprehensive steps, you’ll ensure robust error logging in WordPress—leading to improved stability, security and performance.


Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *