Title: WPCS and Logging Errors
Last modified: November 2, 2023

---

# WPCS and Logging Errors

 *  [KratosGemini](https://wordpress.org/support/users/kratosgemini/)
 * (@kratosgemini)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/wpcs-and-logging-errors/)
 * I’ve been using the [WordPress Coding Standards sniffer](https://github.com/WordPress/WordPress-Coding-Standards)
   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)

 *  [tianna1](https://wordpress.org/support/users/tianna1/)
 * (@tianna1)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/wpcs-and-logging-errors/#post-17171482)
 * 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 code`
       define('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 code`error_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](https://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](https://wordpress.org/support/users/kratosgemini/)
 * (@kratosgemini)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/wpcs-and-logging-errors/#post-17179379)
 * [@tianna1](https://wordpress.org/support/users/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://wordpress.org/documentation/article/debugging-in-wordpress/#wp_debug_log](https://wordpress.org/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.

## Tags

 * [log](https://wordpress.org/support/topic-tag/log/)
 * [logging](https://wordpress.org/support/topic-tag/logging/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [KratosGemini](https://wordpress.org/support/users/kratosgemini/)
 * Last activity: [2 years, 6 months ago](https://wordpress.org/support/topic/wpcs-and-logging-errors/#post-17179379)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
