Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Kļūdu logošanas klase
- *
- * Lai izmantotu šo skriptu nepieciešams izpildīt:
- * set_error_handler(array("ErrorHandler", "handleError"), E_ALL);
- * register_shutdown_function(array("ErrorHandler", "handleShutdown"));
- *
- * @Author Mārtiņš Briedis
- * @Date: 2011-08-07
- */
- class ErrorHandler{
- /**
- * Mape kurā tiks rakstīti logi
- * @var string
- */
- public static $logs_dir = "";
- /**
- * Apstrādājam erroru, veicam logošanu
- * @static
- * @param int $errno
- * @param string $errstr
- * @param string $errfile
- * @param int $errline
- * @return bool
- */
- public static function handleError($errno, $errstr, $errfile, $errline){
- $errors = array(
- E_WARNING => "WARNING",
- E_NOTICE => "NOTICE",
- E_ERROR => "ERROR",
- E_CORE_ERROR => "CORE_ERROR",
- E_COMPILE_ERROR => "COMPILE_ERROR",
- E_USER_ERROR => "USER_ERROR",
- E_USER_NOTICE => "USER_NOTICE",
- );
- $err_type = array_key_exists($errno, $errors) ? $errors[$errno] : "ERROR:$errno";
- self::writeLog(date("[H:i:s]") . " [$err_type] $errstr in $errfile:$errline (" . Utils::IP()
- . ") GET:{" . self::getReadableArray($_GET)
- . "} POST:{" . self::getReadableArray($_POST) . "}\n");
- //Lai tiktu izsaukts arī noklusētais php error logeris
- return false;
- }
- /**
- * Apstrādājam iespējamu skripta pārtraukumu fatal errora dēļ.
- * @static
- * @return void
- */
- public static function handleShutdown(){
- if($error = error_get_last()){
- switch($error['type']){
- case E_ERROR:
- case E_CORE_ERROR:
- case E_COMPILE_ERROR:
- case E_USER_ERROR:
- self::handleError($error['type'], $error['message'], $error['file'], $error['line']);
- break;
- }
- }
- }
- /**
- * Ierakstam ziņojumu log failā
- * @static
- * @param string $message
- * @return void
- */
- private static function writeLog($message){
- $log_dir = self::$logs_dir . date("Y/m/");
- $log_file = $log_dir . date("d") . ".log";
- //Ja mape neeksistē, mēģinam to izveidot (rekursīvi)
- if(!file_exists($log_dir) || !is_dir($log_dir)){
- mkdir($log_dir, 0777, true);
- }
- if(is_writable($log_dir)){
- if(file_put_contents($log_file, $message, FILE_APPEND)){
- chmod($log_file, 0777);
- return true;
- }
- }
- return false;
- }
- /**
- * Reģistrējam error handleri un shutdown funkciju
- * @static
- * @param string $logs_dir Mape kurā galbāsies log faili (ar "/" beigās)
- * @return void
- */
- public static function registerErrorHandlerShutdownFunction($logs_dir){
- self::$logs_dir = $logs_dir;
- set_error_handler(array(__CLASS__, "handleError"), E_ALL);
- register_shutdown_function(array(__CLASS__, "handleShutdown"));
- }
- /**
- * Noformatējam masīvu lasāmā formātā
- * @static
- * @param array $array
- * @return string Formatēta virkne
- */
- private static function getReadableArray($array){
- $str = array();
- if(!is_array($array)){
- return "";
- }
- foreach($array as $key => $val){
- if(is_array($val)){
- $val = "{" . self::getReadableArray($val) . "}";
- }
- $str[] = "$key=$val";
- }
- return implode(";", $str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment