5

How can errors, specific to a given plugin, be logged to an error_log file contained within the plugin root folder itself?

Examples:

  • Syntax errors
  • Database errors
  • Compatibility errors
  • PHP fatal errors
  • Ajax request errors

Similar to the /wp-content/debug.log file enable when WP_DEBUG_LOG is defined as true in the wp-config.php file (albeit specifically for plugins with its concerns separated from any other general error unrelated to the plugin itself).

Research:

Debugging Plugins section in the official documentation of the Codex refers to using 3rd party plugins to debug 3rd party plugins.

Don’t Develop Without Debuggingby Smashing Magazine refers to site-wide general debugging.

Plugin Handbook appears to layout no specific guidelines.

1 Answer 1

12

By setting the second argument to 3 for error_log:

$pluginlog = plugin_dir_path(__FILE__).'debug.log';
$message = 'SOME ERROR'.PHP_EOL;
error_log($message, 3, $pluginlog);

http://php.net/manual/en/function.error-log.php

6
  • I can't be as responsive as I'd like to be, but I'll explore this solution and revert with feedback when possible. Commented Jan 16, 2018 at 6:49
  • By all indications, and upon further reading on the provided source documentation, this appears to be the correct answer for the scope defined in the problem statement. Error handling is typically more verbose, but that would be off-topic for the issue described here, and I believe this will work well with exception handling using try() and catch() blocks (I'll be exploring this route next...). Commented Jan 16, 2018 at 9:23
  • 1
    yes I figured since you mentioned so many different types of errors you were looking for something as generic as this so you can test and log any message where needed. Apart from exceptions as you mention, the PHP fatal errors would require some extra handling via register_shutdown_function or using the shutdown hook, eg. add_action('shutdown', 'custom_catch_fatals');
    – majick
    Commented Jan 16, 2018 at 9:35
  • Thanks for mentioning this, I'll explore these methods as well. Commented Jan 16, 2018 at 9:53
  • I used some of this stuff in a project recently so you may find it useful for reference: gist.github.com/majick777/6c4e4074ce4a59fe09f7baa855732aee
    – majick
    Commented Jan 16, 2018 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.