Logging Errors and detecting Exceptions

Tideways detects uncaught exceptions and fatal errors by default including most exceptions that are handled by supported frameworks and converted into error pages. If your application generates errors another way than throwing Exceptions and handling them centrally, then you need to instrument your code a little to get Exception/Error tracking working.

Logging Exceptions

If you are about to abort a request because of an exception and what it logged, then use the following function on Tideways\Profiler object to log this exception to Tideways backend:

<?php

try {

} catch (\Exception $e) {
    if (class_exists('Tideways\Profiler')) {
        \Tideways\Profiler::logException($e);
    }

    renderErrorPage(); // your code rendering an error page
}

Logging Non-Exception Errors

For errors that are not an exception in your code, there is a function that logs them to Tideways backend:

<?php

if (class_exists('Tideways\Profiler')) {
    \Tideways\Profiler::logFatal($message, $file, $line, $typeOrClass, $frames);
}

Detecting Exceptions

If a function in your application or the framework you use handles exceptions centrally, you can hook into it as a so called exception function, and don’t need to change its code to get Tideways support working. In your bootstrap script configure this feature the following way:

<?php

if (class_exists('Tideways\Profiler')) {
    \Tideways\Profiler::detectExceptionFunction("MyCode::logException");
}

// for a method in your code base that looks like this:
class MyCode {
   public function logException(Exception $e) {
   }
}

Or if your application catches and processes exceptions, then you can help Tideways find those exceptions and collect them. For this task you can pass the name of a function that is called with the Exception as any of the arguments:

<?php

function my_exception_handler(\Exception $e) {
    header("HTTP/1.1 500 Internal Server Error");
    exit;
}

if (class_exists('Tideways\Profiler')) {
    \Tideways\Profiler::detectExceptionFunction('my_exception_handler');
}

This is not necessary to call for frameworks with automatic detection of exceptions.

Still need help? Email [email protected]