Skip to content

WP_DEBUG in production: log without leaking

WP_DEBUG is useful in production, but only with display_errors off and the log file outside the webroot. Here is the pattern, the redaction layer, and the rotation policy.

A customer reports their plugin throws a fatal on a specific URL. Your support engineer says “turn on WP_DEBUG and reproduce it”. Six hours later the page footer is showing the database hostname, the absolute path of the WordPress install, the table prefix, and a stack trace including a third-party SaaS API key that one plugin logs into error_log on every request. The fatal got fixed; the leak went into the indexed Google cache for ninety days.

What’s happening

WP_DEBUG is one constant that controls four behaviours, and the defaults are wrong for production. Setting WP_DEBUG = true does all of:

  1. Raises PHP’s error_reporting to E_ALL so notices, warnings, and deprecation messages all surface.
  2. Enables WP_DEBUG_DISPLAY by default — which renders those errors inline in the HTML output, in front of every visitor.
  3. Stops WordPress from suppressing PHP’s display_errors ini directive.
  4. Allows WP_DEBUG_LOG to write to a file, but defaults the path to wp-content/debug.log — which is inside the webroot and reachable over HTTP unless explicitly blocked.

The three separate constants exist precisely so you can decouple “see errors” from “show errors to the public” from “log errors persistently.” Production wants logging on, display off, and the log file outside any directory the web server is willing to serve.

There’s also a quieter failure mode: poorly written plugins call error_log($var) directly with PII, API keys, customer email addresses, or SQL fragments that contain customer data. If your debug log lives inside wp-content/uploads/, you’ve just built a log aggregation pipeline that publishes to the internet. We’ve seen logs containing Stripe webhook bodies, JWT bearer tokens, and reset-password URLs.

How to verify

Confirm the current state of all four flags:

cd /home/<account>/public_html

wp config get WP_DEBUG         # should be 1 in production only when needed
wp config get WP_DEBUG_DISPLAY # MUST be empty or 0
wp config get WP_DEBUG_LOG     # should be an absolute path outside webroot

# What does PHP actually think?
wp eval 'var_dump(ini_get("display_errors"), ini_get("log_errors"), ini_get("error_log"));'

If display_errors is anything other than string(0) "" or string(1) "0", errors are reaching the browser. Then check whether the log path is reachable:

# If WP_DEBUG_LOG is the default (wp-content/debug.log), test exposure:
curl -sI https://example.com/wp-content/debug.log | head -1
# A 200 OK here is a data leak. 403 or 404 is what you want.

Audit what’s being logged right now:

tail -200 /var/log/wordpress/debug.log | \
  grep -iE "password|token|secret|api[_-]?key|authorization|bearer|stripe|sk_(live|test)|email|@" | \
  head -50

If that returns hits, you have sensitive data in the log and the rotation policy needs to assume the log itself is now a secret.

The fix

The production-safe block (drop into wp-config.php above That's all, stop editing!):

// --- Diagnostic mode, never display, log outside webroot ---
define( 'WP_DEBUG',         true );   // toggle off when you don't need it
define( 'WP_DEBUG_DISPLAY', false );  // NEVER true in production
define( 'WP_DEBUG_LOG',     '/var/log/wordpress/' . $_SERVER['HTTP_HOST'] . '/debug.log' );

// Belt and braces — even if a plugin enables display_errors, suppress it
@ini_set( 'display_errors', 0 );
@ini_set( 'log_errors',     1 );

// Separate the script-error log path from PHP's default
@ini_set( 'error_log', '/var/log/wordpress/' . $_SERVER['HTTP_HOST'] . '/php-errors.log' );

// SAVEQUERIES is brutal on memory — only enable when actively debugging a slow query
// define( 'SAVEQUERIES', true );

Then set up the log directory with the right ownership and rotation:

sudo mkdir -p /var/log/wordpress/example.com
sudo chown nobody:nobody /var/log/wordpress/example.com
sudo chmod 0750 /var/log/wordpress/example.com

# /etc/logrotate.d/wordpress
cat <<'EOF' | sudo tee /etc/logrotate.d/wordpress
/var/log/wordpress/*/*.log {
    daily
    rotate 14
    compress
    delaycompress
    notifempty
    missingok
    create 0640 nobody nobody
    sharedscripts
    postrotate
        /bin/kill -USR1 $(cat /var/run/php-fpm/php-fpm.pid 2>/dev/null) 2>/dev/null || true
    endscript
}
EOF

Block direct HTTP access defensively even if the file ever ends up inside the webroot:

# nginx — drop into the server block
location ~* /(debug\.log|php-errors\.log|error_log)$ {
    deny all;
    return 404;
}
# Apache — drop into .htaccess at the docroot
<FilesMatch "^(debug\.log|php-errors\.log|error_log)$">
    Require all denied
</FilesMatch>

For the redaction layer — a small mu-plugins/00-debug-redact.php that scrubs known-sensitive patterns before they reach the log:

<?php
// /wp-content/mu-plugins/00-debug-redact.php
if ( ! defined('ABSPATH') ) exit;

// Hook into PHP's error handler at the WordPress level
set_error_handler( function ( $severity, $message, $file, $line ) {
    $patterns = [
        '/sk_(live|test)_[A-Za-z0-9]+/'              => 'sk_***REDACTED***',
        '/Bearer\s+[A-Za-z0-9._-]+/'                 => 'Bearer ***REDACTED***',
        '/password["\']?\s*[:=]\s*["\']?[^"\'\s,]+/i'=> 'password=***REDACTED***',
        '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/' => '<email-redacted>',
    ];
    $message = preg_replace( array_keys($patterns), array_values($patterns), $message );
    error_log( "[$severity] $message in $file:$line" );
    return true; // let WordPress' own handler also run
}, E_ALL & ~E_NOTICE & ~E_DEPRECATED );

The simplest rule: never leave WP_DEBUG on for longer than the active investigation needs. Wrap it in a cron-bumped maintenance window or in a feature flag tied to an environment variable so it auto-disables.


Stack Harbor manages WordPress runtime configuration including log routing, redaction, and retention policy as part of enterprise WordPress support operations. If your debug log has been writing to the webroot for longer than you’d like, we can rotate the secrets and lock it down.