briedis

Custom error logger

Jan 18th, 2012
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Kļūdu logošanas klase
  4.  *
  5.  * Lai izmantotu šo skriptu nepieciešams izpildīt:
  6.  * set_error_handler(array("ErrorHandler", "handleError"), E_ALL);
  7.  * register_shutdown_function(array("ErrorHandler", "handleShutdown"));
  8.  *
  9.  * @Author Mārtiņš Briedis
  10.  * @Date: 2011-08-07
  11.  */
  12. class ErrorHandler{
  13.     /**
  14.      * Mape kurā tiks rakstīti logi
  15.      * @var string
  16.      */
  17.     public static $logs_dir = "";
  18.  
  19.     /**
  20.      * Apstrādājam erroru, veicam logošanu
  21.      * @static
  22.      * @param int $errno
  23.      * @param string $errstr
  24.      * @param string $errfile
  25.      * @param int $errline
  26.      * @return bool
  27.      */
  28.     public static function handleError($errno, $errstr, $errfile, $errline){
  29.         $errors = array(
  30.             E_WARNING => "WARNING",
  31.             E_NOTICE => "NOTICE",
  32.             E_ERROR => "ERROR",
  33.             E_CORE_ERROR => "CORE_ERROR",
  34.             E_COMPILE_ERROR => "COMPILE_ERROR",
  35.             E_USER_ERROR => "USER_ERROR",
  36.             E_USER_NOTICE => "USER_NOTICE",
  37.         );
  38.         $err_type = array_key_exists($errno, $errors) ? $errors[$errno] : "ERROR:$errno";
  39.  
  40.         self::writeLog(date("[H:i:s]") . " [$err_type] $errstr in $errfile:$errline (" . Utils::IP()
  41.                 . ") GET:{" . self::getReadableArray($_GET)
  42.                 . "} POST:{" . self::getReadableArray($_POST) . "}\n");
  43.  
  44.         //Lai tiktu izsaukts arī noklusētais php error logeris
  45.         return false;
  46.     }
  47.  
  48.     /**
  49.      * Apstrādājam iespējamu skripta pārtraukumu fatal errora dēļ.
  50.      * @static
  51.      * @return void
  52.      */
  53.     public static function handleShutdown(){
  54.         if($error = error_get_last()){
  55.             switch($error['type']){
  56.                 case E_ERROR:
  57.                 case E_CORE_ERROR:
  58.                 case E_COMPILE_ERROR:
  59.                 case E_USER_ERROR:
  60.                     self::handleError($error['type'], $error['message'], $error['file'], $error['line']);
  61.                     break;
  62.             }
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * Ierakstam ziņojumu log failā
  68.      * @static
  69.      * @param string $message
  70.      * @return void
  71.      */
  72.     private static function writeLog($message){
  73.         $log_dir = self::$logs_dir . date("Y/m/");
  74.         $log_file = $log_dir . date("d") . ".log";
  75.  
  76.         //Ja mape neeksistē, mēģinam to izveidot (rekursīvi)
  77.         if(!file_exists($log_dir) || !is_dir($log_dir)){
  78.             mkdir($log_dir, 0777, true);
  79.         }
  80.  
  81.         if(is_writable($log_dir)){
  82.             if(file_put_contents($log_file, $message, FILE_APPEND)){
  83.                 chmod($log_file, 0777);
  84.                 return true;
  85.             }
  86.         }
  87.         return false;
  88.     }
  89.  
  90.     /**
  91.      * Reģistrējam error handleri un shutdown funkciju
  92.      * @static
  93.      * @param string $logs_dir Mape kurā galbāsies log faili (ar "/" beigās)
  94.      * @return void
  95.      */
  96.     public static function registerErrorHandlerShutdownFunction($logs_dir){
  97.         self::$logs_dir = $logs_dir;
  98.         set_error_handler(array(__CLASS__, "handleError"), E_ALL);
  99.         register_shutdown_function(array(__CLASS__, "handleShutdown"));
  100.     }
  101.  
  102.     /**
  103.      * Noformatējam masīvu lasāmā formātā
  104.      * @static
  105.      * @param array $array
  106.      * @return string Formatēta virkne
  107.      */
  108.     private static function getReadableArray($array){
  109.         $str = array();
  110.         if(!is_array($array)){
  111.             return "";
  112.         }
  113.         foreach($array as $key => $val){
  114.             if(is_array($val)){
  115.                 $val = "{" . self::getReadableArray($val) . "}";
  116.             }
  117.             $str[] = "$key=$val";
  118.         }
  119.         return implode(";", $str);
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment