Heim >php教程 >php手册 >【转】PHP错误处理写入日志记录

【转】PHP错误处理写入日志记录

WBOY
WBOYOriginal
2016-06-06 19:58:011060Durchsuche

?php /** * PHP错误处理写入日志记录 * @author: lnnujxxy * @time: 2009-09-23 * //使用方法: * require './error_handler.php'; * $errorHandler = ErrorHandler::getInstance(); * $errorHandler-logFile = dirname(__FILE__).'/log.php'; * $errorHandl

/**
* PHP错误处理写入日志记录
* @author: lnnujxxy
* @time: 2009-09-23
* //使用方法:
* require './error_handler.php';
* $errorHandler = ErrorHandler::getInstance();
* $errorHandler->logFile = dirname(__FILE__).'/log.php';
* $errorHandler->level = array(E_ERROR, E_WARNING);
* 此时PHP运行有错误发生,错误日志将记录到制定日志目录(需要保证PHP有对日志所在目录操作权限)
* 注:记录错误的级别可以根据需要修改。
*/
class ErrorHandler {
public static $instance = null;
public static $var;
private static $logFile = '';
private static $level = array();

public function __construct() {

   set_error_handler(array('ErrorHandler', 'errorHandler'));
}

public function __set($var, $val) {
   self::$$var = $val;
}

public function __get($var) {
   if(isset(self::$$var)) {
      return self::$$var;
   } else {
      throw new Exception("Property $var does not exist");
   }
}

public static function getInstance(){
   if(self::$instance === null) {
      self::$instance = new self();
   }
   return self::$instance;
}

public static function errorHandler($error,$message,$file,$line) {
   if($error && in_array($error, self::$level)) {
    $log=array();
    switch($error) {
     case E_ERROR:
      $type='ERROR';
      break;
     case E_WARNING:
      $type='WARNING';
      break;
     case E_NOTICE:
      $type='NOTICE';
      break;
     default:
      $type='Unknown error type ['.$error.']';
      break;
    }
    $log[] = date('Y-m-d H:i:s', time())."\t".$type.': '.$message.' in line '.$line.' of file '.$file.', PHP '.PHP_VERSION.' ('.PHP_OS.')';
    if(function_exists('debug_backtrace')) {
     $backtrace=debug_backtrace();
     for($level=1;$level       $message='File: '.$backtrace[$level]['file'].' Line: '.$backtrace[$level]['line'].' Function: ';
      if(IsSet($backtrace[$level]['class']))
       $message.='(class '.$backtrace[$level]['class'].') ';
      if(IsSet($backtrace[$level]['type']))
       $message.=$backtrace[$level]['type'].' ';
      $message.=$backtrace[$level]['function'].'(';
      if(IsSet($backtrace[$level]['args'])) {
       for($argument=0;$argument         if($argument>0)
         $message.=', ';
        $message.=serialize($backtrace[$level]['args'][$argument]);
       }
      }
      $message.=')';
      $log[]=$message;
     }
    }
    self::writeLog(self::$logFile, $log);
   }
}

public static function writeLog($logFile, $log) {
   if(file_exists($logFile) && filesize($logFile) > 2048000) { //2M一个日志文件
    $logDir = $logFileBak = $file = '';
    $logDir = dirname($logFile);
    $file = basename($logFile);
    $logFileBak = $logDir.'/'.$file.date('Y-m-d-H-i-s', time()).'.php';
    rename($logFile, $logFileBak);
   }
   if($fp = fopen($logFile, 'a')) {
    flock($fp, 2);
    $log = is_array($log) ? $log : array($log);
    foreach($log as $tmp) {
     fwrite($fp, "\t".str_replace(array('', '?>'), '', $tmp)."\n");
    }
    fclose($fp);
   }
}
}

/*
$errorHandler = ErrorHandler::getInstance();
$errorHandler->logFile = dirname(__FILE__).'/log.php';
$errorHandler->level = array(E_ERROR, E_WARNING);
str_replace(asdasdf);
*/
?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn