Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vanjor
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanjor/73f48edb69474fd554dd to your computer and use it in GitHub Desktop.
Save vanjor/73f48edb69474fd554dd to your computer and use it in GitHub Desktop.
Php synchronized log system error after request for Codeigniter 2
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* php synchronized log system error after request
* WARNING : only suitable for runing FastCGI model by PHP
* @reference: http://huoding.com/2011/04/12/63
*/
class Php_error_log_collect_hook extends MY_Log {
public static $level = array(
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice'
);
public static $level_mapping = array(
E_ERROR => 'ERROR',
E_WARNING => 'WARN',
E_PARSE => 'ERROR',
E_NOTICE => 'INFO',
E_CORE_ERROR => 'ERROR',
E_CORE_WARNING => 'WARN',
E_COMPILE_ERROR => 'ERROR',
E_COMPILE_WARNING => 'WARN',
E_USER_ERROR => 'ERROR',
E_USER_WARNING => 'WARN',
E_USER_NOTICE => 'INFO',
E_STRICT => 'INFO'
);
public function __construct() {
parent::__construct();
}
public function callback() {
register_shutdown_function(array($this, 'handle_fatal_error'));
}
public function handle_fatal_error() {
if ($last_error = error_get_last()) {
$error_no = $last_error['type'];
if (isset(self::$level_mapping[$error_no]) && self::$level_mapping[$error_no] === 'ERROR') {
$log_info = "PHP Fatal Error: " . $last_error['message'] . " " . $last_error['file'] . " in line " . $last_error['line'];
$this -> write_log("ERROR", $log_info);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment