Cristiano Magro
2017-10-02 c32d4271ce29d5e8391dbd84e9be3916164f9d43
fatal error handler da utilizzare nello shutdown dello script php

viene lanciato in caso di errore bloccante in fase di shoutdown dell'esecuzione

intercetto il messaggio di errore e lo registro sul database
1 files added
1 files modified
84 ■■■■ changed files
src/Vola/FatalHandler/FatalHandler.php 60 ●●●● patch | view | raw | blame | history
tests/test_fatal.php 24 ●●●●● patch | view | raw | blame | history
src/Vola/FatalHandler/FatalHandler.php
@@ -1,27 +1,53 @@
<?php
if (!function_exists("fatal_handler")) {
    function fatal_handler() {
        $errfile = "unknown file";
        $errstr = "shutdown";
        $errno = E_CORE_ERROR;
        $errline = 0;
    function fatal_handler() {
        $error = error_get_last();
        $errfile = "unknown file";
        $errstr = "shutdown";
        $errno = E_CORE_ERROR;
        $errline = 0;
        if ($error['type'] === E_ERROR || $error['type'] === E_CORE_ERROR) {
            $result = print_r($error, true);
        $error = error_get_last();
            $headers = "From: assistenza.pro@vola.it\r\n";
            $headers .= "Subject: Fatal error rilevato\r\n";
            //$headers .= "MIME-Version: 1.0\r\n";
            //$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $listErroriTracciati = array(E_ERROR, E_COMPILE_ERROR, E_CORE_ERROR);
            error_log($result, 1, "cristiano.magro@vola.it", $headers);
        }
    }
        if (in_array($error['type'], $listErroriTracciati)) {
            $result = print_r($error, true);
            $resultEsca = mysql_real_escape_string($result);
            $nomeServer = $_SERVER['SERVER_NAME'];
            if (class_exists('PDOMgr')) {
                try {
                    $db = PDOMgr::create('127.0.0.1', 'censimento_siti', 'root', ''  );
    register_shutdown_function("fatal_handler");
                    $sql =<<<ENDSQL
                            INSERT INTO log_errori_server
                                (server,livello, messaggio, dt)
                                    VALUES (
                                        '$nomeServer',
                                        'FATAL',
                                        '$resultEsca',
                                        now()
                            );
ENDSQL;
                    $db->insert($sql);
                } catch (Exception $exc) {
                    error_log($exc->getTraceAsString());
                }
            } else {
                //in mancanza di PDOMgr mando la mail
                $headers = "From: assistenza.pro@vola.it\r\n";
                $headers .= "Subject: Fatal error rilevato\r\n";
                error_log($result, 1, "cristiano.magro@vola.it", $headers);
            }
        }
    }
    register_shutdown_function("fatal_handler");
}
tests/test_fatal.php
New file
@@ -0,0 +1,24 @@
<?php
//devo avere un livello di errore adeguato perchè vengano tracciati gli errori
error_reporting(E_ALL);
error_log(' --- inizio ---');
include __DIR__ . '/../src/Vola/PDOMgr/bootstrap.php';
include __DIR__ . '/../src/Vola/FatalHandler/FatalHandler.php';
ini_set('display_errors', '1');
//require_once  __DIR__ . '/../src/Vola/FatalHandler/FatalHandler1234.php';
try {
    throw new Exception ("primo saltato");;
} catch (Exception $ex) {
    echo "primo gestito";
}
throw new Exception ("errorissimo");
echo "fine";