☰
WORDPRESS / PRACTICE

Safe Production Debug Logging in WordPress Without Exposing Errors

Safe Production Debug Logging in WordPress Without Exposing Errors

WordPress debugging is sometimes necessary on a production site when white screens, plugin errors, post-update issues, or unstable features occur. However, enabling debug carelessly can turn technical troubleshooting into an exposure of internal information.

The core principle of safe production debugging is to write errors to a controlled log without including them in the HTML response sent to visitors. You should also protect the log file itself, control its size, and disable debugging after the investigation is complete.

Why WP_DEBUG Is Unsafe Without Additional Configuration

WP_DEBUG enables WordPress's diagnostic mode and helps record PHP warnings, deprecated notices, plugin errors, and theme issues. This mode alone does not necessarily mean that visitors will see the messages. The risk appears when errors are also displayed on the page.

Error messages may contain stack traces, absolute server paths, database table names, fragments of SQL queries, request parameters, email addresses, or other operational data. Even if this information seems insignificant, it can help an attacker understand the site's structure and identify a weakness.

On production, detailed error output should not be the primary troubleshooting method. Visitors should see a properly rendered page or a general temporary-error message, while technical details should be written to a restricted log.

  • WP_DEBUG enables diagnostic mode.
  • WP_DEBUG_DISPLAY determines whether errors are shown in the page response.
  • WP_DEBUG_LOG writes messages to a log.
  • display_errors is a system-level PHP option that can also affect error output.

Basic Configuration for Silent Logging

For temporary troubleshooting, you can use a configuration in wp-config.php that records errors without displaying them to visitors. Place these definitions before the line where WordPress finishes loading the configuration. Do not create duplicate definitions of the same constants later in the file, because the effective value may become unexpected.

A value of true for WP_DEBUG_LOG usually sends the log to wp-content/debug.log. In environments with stricter security requirements, consider using a separate path outside the public directory if your WordPress and server configuration supports it.

  • Confirm that the web server user can create or append to the log file.
  • Do not enable WP_DEBUG_DISPLAY on production.
  • After changing the configuration, reproduce the issue and check whether a new log entry appears.
  • Back up wp-config.php before editing it and check the PHP syntax.
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', true );
@ini_set( 'display_errors', 0 );

How to Verify That Errors Are Not Included in the Page

After enabling logging, open the problematic page in a separate private window or perform a controlled request using administrative access. Confirm that the HTML does not contain PHP Warning, Fatal error, stack traces, or file paths.

Do not check only the home page. An error may occur in an AJAX request, REST API response, form, cart, payment process, or cron job. For these scenarios, examine the HTTP response, status code, and entries in debug.log.

After testing, review the log and match the entry time to the moment when you reproduced the issue. This helps distinguish a current error from older messages that remained in the file.

  • Check the page as an unauthenticated user.
  • Review the source HTML and responses in the browser's Network tab.
  • Record the test time and the URL that triggered the error.
  • After troubleshooting, clear or move old entries so they are not confused with new ones.

Protecting wp-content/debug.log

You should not consider debug.log safe simply because it has a .log extension. If it is accessible through the web server, an external user may try to open it directly. Even a 403 response should not be the only protection layer: access to the log is best restricted at the filesystem, web server, and storage-structure levels.

The best option is to write the log to a directory that is not publicly accessible. If this is not possible, configure the web server to deny direct HTTP access to debug.log. The specific rule depends on Apache, Nginx, your hosting provider, and other components, so test it with a controlled request rather than copying it without adaptation.

Only administrators or specialists performing the troubleshooting should have access to the server and logs. Do not include debug.log in public backups, repositories, or archives accessible through a security panel.

  • Use permissions that prevent arbitrary system users from modifying or reading the file.
  • Test the direct URL to debug.log from both authenticated and unauthenticated sessions.
  • Do not send the complete log in public chats or tickets without cleaning it first.
  • If the log was publicly accessible, treat it as potentially compromised and check for tokens and other secrets.

Controlling Log Size and Content

Errors in a loop or repeated messages from a plugin can quickly increase the size of debug.log. A large file consumes disk space, makes searching more difficult, and may affect site operation. Before enabling debug, decide how you will control the log: manual clearing for a short investigation or log rotation through your hosting environment or operating system.

The log may contain more than technical function names. It can sometimes include request parameters, email addresses, order IDs, URLs with tokens, and fragments of form data. Before sending logs to a contractor, replace or remove these values.

Do not use debug.log as a permanent audit store. Long-term monitoring requires separate tools with a retention policy, access controls, and personal-data masking.

  • Check the file size during active troubleshooting.
  • Archive the log only in secure storage.
  • Delete or clear the log after the work is complete.
  • Do not log passwords, API keys, cookies, payment data, or complete access tokens.

Targeted Tracing with error_log()

If you need to inspect a specific part of your own code, it is better to add short diagnostic entries with error_log() than to enable detailed output across the entire site. This approach helps you determine whether a function runs, what value a safe operational parameter has, and at which stage the problem occurs.

Log only the minimum information required. Do not record an entire request array or user object without filtering it, because it may contain personal data and secrets. Before using this approach in production, remove temporary entries or restrict them with a condition that is activated only during a controlled test.

  • Log an object or order ID instead of the complete data array.
  • For arrays, use a limited representation containing only the required fields.
  • Add a clear prefix so your own entries can be found quickly.
  • Do not leave diagnostic entries permanently in the code without explanation and control.
error_log( '[my-plugin] Початок обробки замовлення: ' . (int) $order_id );

if ( ! $result ) {
    error_log( '[my-plugin] Обробка замовлення завершилася помилкою' );
}

Safe Production Debugging: Recommended Sequence

Troubleshooting on a live site should be performed in short, controlled sessions. First, document the problem and back up the configuration. Then enable logging without output, reproduce the error once or a few times, and immediately analyze the new entries.

If the cause is related to a plugin or theme, check compatibility, recent changes, and the PHP log at the hosting level. Do not automatically delete messages before preserving the context of the problem. At the same time, do not leave debug enabled after troubleshooting is complete.

  • Record the URL, user action, and approximate time of the error.
  • Enable WP_DEBUG without displaying messages.
  • Reproduce only the required scenario.
  • Check debug.log and server-level PHP logs.
  • Fix the cause, clear the log, and disable debug.
  • Test the page again as both an administrator and a regular visitor.
define( 'WP_DEBUG', false );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', false );

Frequently asked questions

Can WP_DEBUG be left enabled on production?

Temporarily, yes, if errors are logged without being displayed, access to the log is protected, and its size and contents are controlled. After troubleshooting, it is best to disable debug so unnecessary or sensitive data does not continue to accumulate.

Does WP_DEBUG automatically show errors to visitors?

Not necessarily. Output depends on WP_DEBUG_DISPLAY and the PHP display_errors setting. On production, explicitly disable error display and verify the actual HTML response.

Where does WordPress store debug.log?

With the default configuration, the log is stored at wp-content/debug.log. The actual path may depend on WordPress, PHP, and hosting settings, so verify it on the specific server.

What should I do if debug.log is not created?

Check the location of the constants in wp-config.php, write permissions for the web server user, available disk space, and server-level PHP logs. Also confirm that you reproduced the error after changing the configuration.

Is it safe to send debug.log to a developer?

Before sending it, review the file and remove tokens, email addresses, cookies, form parameters, SQL fragments, and other personal or secret data. Send only the entries required to analyze the issue.

Conclusion

Safe production debugging in WordPress does not mean abandoning diagnostics; it means separating logging from error display. WP_DEBUG can record technical details, while WP_DEBUG_DISPLAY and display_errors should remain disabled for visitors.

Protect debug.log, limit its contents, control its size, and use targeted error_log() calls for your own code. After testing is complete, clear the log, remove temporary tracing, and disable debug.

← All WordPress materials