亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

php 自定義錯誤日志

 項(xiàng)目中需要對定義錯誤日志及時處理, 那么就需要修改自定義錯誤日志的輸出方式(寫日志、發(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)行的時候可能存在執(zhí)行超時,或強(qiáng)制關(guān)閉等情況,但這種情況下默認(rèn)的提示是非常不友好的,如果使用register_shutdown_function()函數(shù)捕獲異常,就能提供更加友  好的錯誤展示方式,同時可以實(shí)現(xiàn)一些功能的后續(xù)操作,如執(zhí)行完成后的臨時數(shù)據(jù)清理,包括臨時文件等。

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

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

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

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

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

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

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

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

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

相關(guān)文章

最新評論