exceptionloggingphp-7.0

PHP 7 uncaught exceptions aren't logged


I migrate some web applications from PHP 5.6 to PHP 7.0 but I noticed that not handled exceptions won't be logged in the configured log (or at least in the apache log error.log). The error message won't be displayed on the web site, it just stops after the error has been raised.

I tried it with that simple code:


<?php
namespace Application\MyApp;
    
ini_set('error_log', '/my/log/location/my_error.log');
error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
    
$host = gethostname();
echo "<b>start test on $host</b><br>";
    
throw new \Error('Test exception outside of try/catch.');
    
echo "<b>test finished on $host<b><br>";

With PHP 5.6 I get with the same code the expected error message in the configured log:

PHP Fatal error:  Uncaught exception 'Error' with message 'Test exception outside of try/catch.

Is it possible to redirect all uncaught exceptions into the configured log file, as in PHP 5.6?


Solution

  • I get the required logging massages, if I add a exception handler method:

    function myExceptionHandler (\Throwable $ex)
    {
        echo $ex->getMessage();
        throw $ex;
    }
    set_exception_handler('Application\MyApp\myExceptionHandler');