欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php 自定義錯(cuò)誤日志實(shí)例詳解

 更新時(shí)間:2016年11月12日 08:53:45   投稿:lqh  
這篇文章主要介紹了php 自定義錯(cuò)誤日志實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

php 自定義錯(cuò)誤日志

 項(xiàng)目中需要對(duì)定義錯(cuò)誤日志及時(shí)處理, 那么就需要修改自定義錯(cuò)誤日志的輸出方式(寫(xiě)日志、發(fā)郵件、發(fā)短信)

  一. register_shutdown_function(array('phperror','shutdown_function')); //定義PHP程序執(zhí)行完成后執(zhí)行的函數(shù)

    函數(shù)可實(shí)現(xiàn)當(dāng)程序執(zhí)行完成后執(zhí)行的函數(shù),其功能為可實(shí)現(xiàn)程序執(zhí)行完成的后續(xù)操作。程序在運(yùn)行的時(shí)候可能存在執(zhí)行超時(shí),或強(qiáng)制關(guān)閉等情況,但這種情況下默認(rèn)的提示是非常不友好的,如果使用register_shutdown_function()函數(shù)捕獲異常,就能提供更加友  好的錯(cuò)誤展示方式,同時(shí)可以實(shí)現(xiàn)一些功能的后續(xù)操作,如執(zhí)行完成后的臨時(shí)數(shù)據(jù)清理,包括臨時(shí)文件等。

 可以這樣理解調(diào)用條件:

    1、當(dāng)頁(yè)面被用戶(hù)強(qiáng)制停止時(shí)
    2、當(dāng)程序代碼運(yùn)行超時(shí)時(shí)
    3、當(dāng)PHP代碼執(zhí)行完成時(shí),代碼執(zhí)行存在異常和錯(cuò)誤、警告

  二. set_error_handler(array('phperror','error_handler')); // 設(shè)置一個(gè)用戶(hù)定義的錯(cuò)誤處理函數(shù)

    通過(guò) set_error_handler() 函數(shù)設(shè)置用戶(hù)自定義的錯(cuò)誤處理程序,然后觸發(fā)錯(cuò)誤(通過(guò) trigger_error()):

 三. set_exception_handler(array('phperror','appException')); //自定義異常處理

    定義異常拋出的數(shù)據(jù)格式。

class phperror{
  
  //自定義錯(cuò)誤輸出方法
  public static function error_handler($errno, $errstr, $errfile, $errline){
    $errtype = self::parse_errortype($errno);
    $ip = $_SERVER['REMOTE_ADDR'];//這里簡(jiǎn)單的獲取客戶(hù)端IP
    //錯(cuò)誤提示格式自定義
    $msg = date('Y-m-d H:i:s')." [$ip] [$errno] [-] [$errtype] [application] {$errstr} in {$errfile}:{$errline}";
    //自定義日志文件的路徑
    $logPath = 'logs/app.log';
    //寫(xiě)操作,注意文件大小等控制
    file_put_contents($logPath, $msg, FILE_APPEND);
  }

  //系統(tǒng)運(yùn)行中的錯(cuò)誤輸出方法
  public static function shutdown_function(){
    $lasterror = error_get_last();//shutdown只能抓到最后的錯(cuò)誤,trace無(wú)法獲取
    $errtype = self::parse_errortype($lasterror['type']);
    $ip = $_SERVER['REMOTE_ADDR'];//這里簡(jiǎn)單的獲取客戶(hù)端IP
    //錯(cuò)誤提示格式自定義
    $msg = date('Y-m-d H:i:s')." [$ip] [{$lasterror['type']}] [-] [$errtype] [application] {$lasterror['message']} in {$file}:{$lasterror['line']}";
    //自定義日志文件的路徑
    $logPath = 'logs/app.log';
    //寫(xiě)操作,注意文件大小等控制
    file_put_contents($logPath, $msg,FILE_APPEND);
  }

 //自定義異常輸出

  public static function appException($exception) { 
   echo " exception: " , $exception->getMessage(), "/n"; 
  } 
  private static function parse_errortype($type){
    switch($type){
      case E_ERROR: // 1 
        return 'Fatal Error';
      case E_WARNING: // 2 
        return 'Warning';
      case E_PARSE: // 4 
        return 'Parse error';
      case E_NOTICE: // 8 
        return 'Notice';
      case E_CORE_ERROR: // 16 
        return 'Core error';
      case E_CORE_WARNING: // 32 
        return 'Core warning';
      case E_COMPILE_ERROR: // 64 
        return 'Compile error';
      case E_COMPILE_WARNING: // 128 
        return 'Compile warning';
      case E_USER_ERROR: // 256 
        return 'User error';
      case E_USER_WARNING: // 512 
        return 'User warning';
      case E_USER_NOTICE: // 1024 
        return 'User notice';
      case E_STRICT: // 2048 //
        return 'Strict Notice';
      case E_RECOVERABLE_ERROR: // 4096 
        return 'Recoverable Error';
      case E_DEPRECATED: // 8192 
        return 'Deprecated';
      case E_USER_DEPRECATED: // 16384 
        return 'User deprecated';
    }
    return $type;
  }
  
}

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論