From c32d4271ce29d5e8391dbd84e9be3916164f9d43 Mon Sep 17 00:00:00 2001
From: Cristiano Magro <cristiano.magro@vola.it>
Date: Mon, 02 Oct 2017 18:13:33 +0200
Subject: [PATCH] fatal error handler da utilizzare nello shutdown dello script php
---
tests/test_fatal.php | 24 ++++++++++++
src/Vola/FatalHandler/FatalHandler.php | 60 +++++++++++++++++++++--------
2 files changed, 67 insertions(+), 17 deletions(-)
diff --git a/src/Vola/FatalHandler/FatalHandler.php b/src/Vola/FatalHandler/FatalHandler.php
index d7de801..c9dd2b6 100644
--- a/src/Vola/FatalHandler/FatalHandler.php
+++ b/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");
}
diff --git a/tests/test_fatal.php b/tests/test_fatal.php
new file mode 100644
index 0000000..9bc6271
--- /dev/null
+++ b/tests/test_fatal.php
@@ -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";
\ No newline at end of file
--
Gitblit v1.8.0