set_exception_handler ('_ exception_handler2');
set_error_handler ('_ exception_handler');
/ ** * Exception handler * * / function _exception_handler2 ($ errstr) { $ error = & load_class ('Exceptions'); // Display the exception text on the screen echo $ error-> show_error ('error', nl2br ($ errstr)); // Should we log the error? No? We're done ... $ config = & get_config (); if ($ config ['log_threshold'] == 0) { return; } // Write a message to the log $ error-> log_exception ('Exception', $ errstr, '', ''); exit; }
/ * | ------------------------------------------------- ------------------------- | Notification administrator about errors on the site | ------------------------------------------------- ------------------------- | | If you want the administrator to receive letters about errors and exceptions | Specify the following address with the address of the mailbox administrator | * / $ config ['log_to_email'] = 'admin@site.ru';
class MY_Exceptions extends CI_Exceptions { / ** * Exception Logger * * Method replaces basic adding features * send error message to site administrator * and display additional information in the log * * @access private * @param string the error severity * @param string the error string * @param string the error filepath * @param string the error line number * @return string * / function log_exception ($ severity, $ message, $ filepath, $ line) { // Send a message to the site administrator $ this-> adminmail ($ message); $ severity = (! isset ($ this-> levels [$ severity]))? $ severity: $ this-> levels [$ severity]; // Supplement the message text with dumps of environment variables $ message. = "\ n". $ this-> GetGlobalVariables (). $ filepath. ' '. $ line. "\ n \ n"; log_message ('error', 'Severity:'. $ severity. '->'. $ message, TRUE); } / ** * 404 Page Not Found Handler * * The method complements the basic ability to send a message to the administrator * in case of an error * * @access private * @param string * @return string * / function show_404 ($ page = '') { // Send the message $ message = '404 Page Not Found ->'. $ page; $ this-> adminmail ($ message); parent :: show_404 ($ page); } / ** * Returns a dump of environment variables * * @return string dump environment variables * / function GetGlobalVariables () { $ content = 'REMOTE_ADDR ='. $ _ SERVER ["REMOTE_ADDR"]. "\ n"; if (isset ($ _ SERVER ["HTTP_REFERER"])) { $ content. = 'HTTP_REFERER ='. $ _ SERVER ["HTTP_REFERER"]. "\ n"; } $ content. = 'USER_AGENT ='. $ _ SERVER ["HTTP_USER_AGENT"]. "\ n"; $ content. = '$ _SERVER [\' REQUEST_URI \ '] ='; $ content. = var_export (@ $ _ SERVER ['REQUEST_URI'], true); $ content. = "\ n". '$ _ GET ='; $ content. = var_export (@ $ _ GET, true); $ content. = "\ n". '$ _ POST ='; $ content. = var_export (@ $ _ POST, true); $ content. = "\ n"; return $ content; } / ** * The method sends an error message to the mail administrator * * @return * @param object $ message * / function adminmail ($ message) { $ CI = & get_instance (); // If the config is not loaded - we load if (! isset ($ CI-> config)) { $ CI-> config = & load_class ('Config'); } // If the bootloader is not loaded - load if (! isset ($ CI-> load)) { $ CI-> load = & load_class ('Loader'); } // Load the class email if (! isset ($ CI-> email)) { $ CI-> email = & load_class ('Email'); } // For the mail class, the Language class is required. if (! isset ($ CI-> lang)) { $ CI-> lang = & load_class ('Language'); } // Email address of the administrator $ email = $ CI-> config-> item ('log_to_email'); // If the address is not specified - do nothing if (! strlen ($ email)) { return; } // Sending $ CI-> email-> from ('noreply'); $ CI-> email-> to ($ email); $ CI-> email-> subject ('There was an error on the site'); $ CI-> email-> message ($ message); $ CI-> email-> send (); } }
$ config ['enable_hooks'] = TRUE;
$ hook ['pre_system'] = array ( 'class' => '', 'function' => 'addExceptionHandler', 'filename' => 'exception_hook.php', 'filepath' => 'hooks' );
if (! defined ('BASEPATH')) exit ('No direct script access allowed'); / ** * Install an exception handler * * / function addExceptionHandler () { set_exception_handler ('_ exception_handler2'); } / ** * Exception handler * * / function _exception_handler2 ($ errstr) { $ error = & load_class ('Exceptions'); $ error-> adminmail ('1'); // Display the exception text on the screen echo $ error-> show_error ('error', nl2br ($ errstr)); // Should we log the error? No? We're done ... $ config = & get_config (); if ($ config ['log_threshold'] == 0) { return; } // Write a message to the log $ error-> log_exception ('Exception', $ errstr, '', ''); exit; }
Source: https://habr.com/ru/post/74381/