A WordPress debugging setup per environment
Debug settings are toggled in wp-config.php by hand, forgotten on, and then a production site shows notices to visitors or fills a public debug.log. A small setup decides everything from the environment type: on local and staging, full logging into a private file and visible errors where useful; on production, logging without display. A few helper functions make the log useful instead of noisy.
Problem: On a project with local, staging and production environments, WP_DEBUG is changed by hand before each deployment. Twice it was deployed as true, and the log file in wp-content could be downloaded.
Solution: Set WP_ENVIRONMENT_TYPE per server (an environment variable or a server-specific config file), derive all debug constants from it in wp-config.php, write the log outside the web root, add a small ha_log() helper that writes only outside production, and use Query Monitor and Xdebug only on local and staging.
wp-config.php
// wp-config.php, above "That's all, stop editing!"
define( 'WP_ENVIRONMENT_TYPE', getenv( 'WP_ENVIRONMENT_TYPE' ) ?: 'production' );
$ha_env = WP_ENVIRONMENT_TYPE;
define( 'WP_DEBUG', 'production' !== $ha_env );
define( 'WP_DEBUG_LOG', '/var/log/wordpress/' . $ha_env . '-debug.log' );
define( 'WP_DEBUG_DISPLAY', 'local' === $ha_env );
define( 'SCRIPT_DEBUG', 'local' === $ha_env );
define( 'SAVEQUERIES', false );
if ( 'production' === $ha_env ) {
define( 'DISALLOW_FILE_EDIT', true );
@ini_set( 'display_errors', '0' );
}
WP_ENVIRONMENT_TYPE(WordPress 5.5+) acceptslocal,development,stagingandproduction; anything else falls back toproduction, which is the safe default. Set it in the PHP-FPM pool (env[WP_ENVIRONMENT_TYPE] = staging) or the web server, so the samewp-config.phpcan be deployed everywhere.- On production,
WP_DEBUGis off: PHP still logs fatal errors to its own error log, but WordPress’s deprecation notices are silent. WP_DEBUG_LOGwith a path (since 5.1) puts the log outsidewp-content. The folder must be writable by the PHP user; rotation: logrotate for WordPress logs.SCRIPT_DEBUGloads unminified core scripts locally, which makes JavaScript errors in core readable.
A log helper
function ha_log( string $message, array $context = array() ): void {
if ( 'production' === wp_get_environment_type() && ! defined( 'HA_FORCE_LOG' ) ) {
return;
}
$line = $message;
if ( $context ) {
$line .= ' ' . wp_json_encode( $context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
}
error_log( '[ha] ' . $line );
}
ha_log( 'Sync finished', array( 'created' => 12, 'updated' => 340, 'ms' => 8120 ) );
error_log() writes into the file from WP_DEBUG_LOG (WordPress sets PHP’s error_log setting to it). The [ha] prefix makes the plugin’s lines easy to filter: grep '\[ha\]' /var/log/wordpress/staging-debug.log. Never log passwords, tokens or full request bodies; a log is read by more people than the database.
Environment-dependent behavior
if ( 'production' !== wp_get_environment_type() ) {
add_filter( 'wp_mail', function ( array $mail ): array {
$mail['to'] = 'dev@example.com'; // never email real customers from staging
return $mail;
} );
}
The same check disables payment gateways’ live mode, search engine indexing (WordPress itself does not do that automatically) and external webhooks on staging.
The tools per environment
- Query Monitor on local and staging, for queries, hooks, HTTP calls per request: Query Monitor.
- Xdebug only locally (step debugging and profiling slow down every request).
- The PHP error log and the web server log on production, for fatal errors that happen before WordPress is loaded.
- A local environment where all of this is set up once: Docker Compose for local WordPress.
NOTE: When a production problem needs more logs, define HA_FORCE_LOG temporarily or switch WP_DEBUG on with WP_DEBUG_DISPLAY off for a few minutes, and switch it back. Recovery mode (WordPress 5.2+) sends the admin email a link when a plugin causes a fatal error, which is often faster than reading logs.