php教程|php手册
PHP,错误,处理,写入,日志,记录,php,PHP,错误,
php教程-php手册
排队返现源码下载,vscode怎么打开py,ubuntu 单机游戏,tomcat一览,按键精灵sqlite数据库,爬虫的数据如何导入到数据库,php 引用 赋值,武汉seo网络推广优化,win10 建网站,模板函数 回调函数lzw
?php /** * PHP错误处理写入日志记录 * @author: lnnujxxy * @time: -09-23 * //使用方法: * require ‘./error_handler.php’; * $errorHandler = ErrorHandler::getInstance(); * $errorHandler-logFile = dirname(__FILE__).’/log.php’; * $errorHandl
monkey源码位置,ubuntu用户登录失败,爬虫 点击浏览视频,ucword php,seo 黑科技lzw
网站源码微信支付,ubuntu tty2,tomcat七个文件上传,爬虫 换ip,php代码怎么和html分离,seo链接发布lzw
<?php
/**
* PHP错误处理写入日志记录
* @author: lnnujxxy
* @time: -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<count($backtrace);$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<count($backtrace[$level][args]);$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);
*/
?>