• I’ve been using the WordPress Coding Standards sniffer to help me maintain good code and do things the WordPress way. I’m currently developing some code where I want to log certain errors that occur so I can tell when they happen and not just rely on user complaints to come in.

    But I have been unable to figure out how to log errors in production in a way that is compliant with WPCS. It flags both error_log() and trigger_error() as needing to be behind a WP_DEBUG check. But that doesn’t help me when my code is in production and I’m not the one who may come across an error.

    What’s the proper way to log errors in production in a WordPress environment? I feel like I’m missing something simple.

    In case it helps, the use case I’m looking at now is logging an error if an issue occurs with an external API call. I’d still display a graceful message to the user, but I’d like to know if there are unexpected problems with the API calls.

Viewing 2 replies - 1 through 2 (of 2 total)
  • When it comes to logging errors in a production WordPress environment while following the WordPress Coding Standards, you can use the following approach:

    1. Use WP_DEBUG_LOG: WordPress has a built-in feature called WP_DEBUG_LOG that allows you to log errors without displaying them to users. Here’s how to set it up:In your wp-config.php file, add the following lines:phpCopy codedefine('WP_DEBUG', false); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false); With this configuration, errors will be logged to a file in the wp-content directory named debug.log.
    2. Log Errors with error_log(): You can use the error_log() function to log errors in your code. Since you’ve set WP_DEBUG_LOG to true, the errors will be written to the debug.log file without displaying them to users.Example:phpCopy codeerror_log('An error occurred with the external API call: ' . $error_message);

    In case it helps, the use case I’m looking at now is logging an error if an issue occurs with an external API call on keduplicatebill.com.pk. I’d still display a graceful message to the user, but I’d like to know if there are unexpected problems with the API calls.

    Thread Starter KratosGemini

    (@kratosgemini)

    @tianna1

    I appreciate you taking the time to reply. However, in my testing, setting WP_DEBUG_LOG to true while WP_DEBUG is false does not do anything. This matches the documentation, which states as much.

    https://www.ads-software.com/documentation/article/debugging-in-wordpress/#wp_debug_log

    But even if it did work, it doesn’t get around my issue of WPCS warning about using error_log() in production. Also, having WP_DEBUG_LOG write the error log to wp-content is unnecessary in my case because I have access to the server log anyway.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WPCS and Logging Errors’ is closed to new replies.