0

I would like to know is there any way to catch all PHP errors (not only WP Errors) in Wordpress without modifying core?

The reason I am asking is that I would like to manually catch all errors and use third party software as an error aggregator, for example Sentry, but not limited to. for example email them or log to database. I know Wordpress can log errors to file in DEBUG mode but this is not what I am looking for, because it's to simple and limiting.

And yes - I want to catch Wordpress core errors, if there are any. Not only plugin/theme related.

[edit]
This is requirement made by my client - they want me to be able to monitor all PHP related errors in any application they have, not only Wordpress. WP log file is not enough for them, beceause the want to be able to aggregate all their applications (including WP) errors in one place, index them, generate support tickets etc.*

Basically if I could push all errors through Monolog, that'll be perfect. Monolog is not requirement, but it is easy to implement, that's all.

[edit]
I have already tried this gist, but it is limited to only DB operations. Also I've tried (just for educational reasons) to add error handler (Monolog) in main WP index.php file and it works, but I can't figure out how to make WP push all it's errors through it. I know it's wrong to edit an of core files (not upgrade safe etc.), that's why I'm asking if there is any other way.*

Thanks in advance!

  • 3
    Third party tools integration is off-topic unless you can supply the essential details needed to perform it. Alternatively please edit your question and define what exactly you want to do on the wordpress side and what have you tried so far (or options you have researched if it is really hard to understand where to start). – Mark Kaplun Oct 23 '14 at 5:56
  • What do you mean by core errors. AFAIK, there are no php errors in core. Core errors are triggered by errors in plugin and theme code. Error reporting are sometimes off by blaming core code as the culprit whereas it should highlight the actual code in the theme or plugin which caused the error in the firts place. – Pieter Goosen Oct 23 '14 at 6:28
  • Pieter,Honestly I don't think that there will be any WP Core errors whatsoever. This is my client's requirement and I'm trying to figure out if it is at all possible and feasible. – wasil Oct 23 '14 at 6:32
  • IMO, "core error" might be a deprecation notice. never looked deep into what else might generate an error. – Mark Kaplun Oct 23 '14 at 6:45
  • I think that too. Anyway the'd like to catch E_DEPRECATED too if it is possible. – wasil Oct 23 '14 at 6:51
1

Using a tool to monitor itself is always a problematic idea, if wordpress has errors how do you know that those errors do not impact your integration code in a way which prevents them from being reported upstream?

Your best option is probably to setup error reporting levels in php and if applicable in wordpress, and have an external tool to monitor the file and notify upstream when they are changed.

1

Yes, there is a way to catch PHP errors (notice, warning, error) with a custom error handler by using set_error_handler():

$logger = new Monolog\Logger( 'PHP-ERROR' );
$logger->pushHandler(
    // Configure your Monolog handler here
);
set_error_handler( function( int $errNo, string $errStr, ... $args ) use ( $logger ) {
    $errorName = 'NOTICE';
    $errorLevel = Monolog\Logger::NOTICE;
    switch ( $errNo ) {
        case E_USER_WARNING : 
            $errorName = 'WARNING';
            $errorLevel = Monolog\Logger::WARNING;
            break;
        case E_USER_ERROR : 
            $errorName = 'ERROR';
            $errorLevel = Monolog\Logger::ERROR;
            break;
    }

    $errorMessage = "{$errorName} {$errStr}";
    $logger->addRecord( $errorLevel, $errorMessage );

    // return false if you want the default error handler to proceed
    return true;
) };

This is just an untested example but basically the way of how to send PHP errors to Monolog. Thus you can use the power of all the existing Monolog handlers.

If you want to log custom WordPress errors (like DB errors, HTTP errors, failed log ins) you have to listen to the specific hooks of the WP API and delegate messages about these events to Monolog.

However, if you are able to use third-party packages, I would suggest to have a look at Wonolog. A library that aims to connect WordPress to Monolog. It does exactly the same as above plus adds a comfortable API to plugin developers to log anything they like.

Edit: If it is only about PHP errors and Monolog, you can use the error handler shipped with Monolog instead:

$logger = new Monolog\Logger( 'PHP-ERROR' );
(new ErrorHandler( $logger ) )->registerErrorHandler()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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