分享一个php 的异常处理程序


Posted in PHP onJune 22, 2014

直接上代码

<?php
//exceptionHandle.php xiecongwen 20140620
//define('DEBUG',true);
/**
* Display all errors when APPLICATION_ENV is development.
*/
if (defined('DEBUG')) {
error_reporting(E_ALL);
ini_set("display_errors", 1);
}
if(!defined('DEBUG')){
/**
* 当发生重大错误时 写日志 并友好提示用户
* (PS:只所以将代码写在这里,是因为在其他地方注册时,出现问题无法调用配置函数.待完善...)
*/
function shutdownHandler()
{
/**
* 写日志 此处直接写在根目录下shutdownlog.txt
*/
$lasterror = error_get_last();
if($lasterror){
$error = strval(date("Y-m-d h:i:s")).'=>'."[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line']."\n";
file_put_contents('./log/'.date("Ymd").'shutdownlog.txt',$error,FILE_APPEND);
//友好提示用户
ob_end_clean();
die('对不起,我出错了!');
}
}
register_shutdown_function('shutdownHandler');
}
if(!defined('DEBUG')){
 
function errorHandler($errno, $errstr = '', $errfile = '', $errline = 0)
{
//写日志
$exception = new \ErrorException($errstr, 0, $errno, $errfile, $errline);
$msg = strval(date("Y-m-d h:i:s")).'=>'.'Type:'.getErrTypeName($errno).' '.getMsg($exception);
file_put_contents('./log/'.date("Ymd").'error.txt',$msg,FILE_APPEND);
switch ($errno)
{
case E_NOTICE:return ;
case E_DEPRECATED:return;
}
throw $exception;
}
function getErrTypeName($errno)
{
switch ($errno)
{
case E_NOTICE:return 'E_NOTICE' ;
case E_DEPRECATED:return 'E_DEPRECATED';
default:return $errno;
}
}
function exceptionHandler($ex)
{
$msg = strval(date("Y-m-d h:i:s")).'=>'.getMsg($ex);
file_put_contents('./log/'.date("Ymd").'exception.txt',$msg,FILE_APPEND);
}
function getMsg($exception)
{
//获取最准确的异常 
while($exception->getPrevious())$exception = $exception->getPrevious();
$msg = ' Message: '.$exception->getMessage();
$msg .= ' File: '.$exception->getFile().':'.$exception->getLine()."\n";
return $msg;
}
set_error_handler('errorHandler',E_ALL);
set_exception_handler('exceptionHandler');
}
?>
PHP 相关文章推荐
在PHP中使用灵巧的体系结构
Oct 09 PHP
php自定义函数之递归删除文件及目录
Aug 08 PHP
PHP通过iconv将字符串从GBK转换为UTF8字符集
Jul 18 PHP
yii去掉必填项中星号的方法
Dec 28 PHP
简单谈谈php延迟静态绑定
Jan 26 PHP
PHP中PDO连接数据库中各种DNS设置方法小结
May 13 PHP
PHP中如何判断exec函数执行成功?
Aug 04 PHP
在Mac OS下搭建LNMP开发环境的步骤详解
Mar 10 PHP
PHP实现的激活用户注册验证邮箱功能示例
Jun 06 PHP
深入理解PHP中mt_rand()随机数的安全
Oct 12 PHP
redis+php实现微博(一)注册与登录功能详解
Sep 23 PHP
php多进程并发编程防止出现僵尸进程的方法分析
Feb 28 PHP
Yii2使用小技巧之通过 Composer 添加 FontAwesome 字体资源
Jun 22 #PHP
浅析application/x-www-form-urlencoded和multipart/form-data的区别
Jun 22 #PHP
PHP延迟静态绑定示例分享
Jun 22 #PHP
美图秀秀web开放平台--PHP流式上传和表单上传示例分享
Jun 22 #PHP
解决cPanel无法安装php5.2.17
Jun 22 #PHP
destoon设置自定义搜索的方法
Jun 21 #PHP
destoon后台网站设置变成空白的解决方法
Jun 21 #PHP
You might like
PHP中调用JAVA
2006/10/09 PHP
php输入流php://input使用示例(php发送图片流到服务器)
2013/12/25 PHP
PHP基于工厂模式实现的计算器实例
2015/07/16 PHP
php实现网站留言板功能
2015/11/04 PHP
PHP基于PDO实现的SQLite操作类【包含增删改查及事务等操作】
2017/06/21 PHP
理解 JavaScript 预解析
2009/10/25 Javascript
ExtJs之带图片的下拉列表框插件
2010/03/04 Javascript
js下将阿拉伯数字每三位一逗号分隔(如:15000000转化为15,000,000)
2014/06/02 Javascript
AngularJs根据访问的页面动态加载Controller的解决方案
2015/02/04 Javascript
JavaScript+html5 canvas绘制渐变区域完整实例
2016/01/26 Javascript
Vue.js bootstrap前端实现分页和排序
2017/03/10 Javascript
BootStrap表单时间选择器详解
2017/05/09 Javascript
详解webpack2+node+react+babel实现热加载(hmr)
2017/08/24 Javascript
vue开发拖拽进度条滑动组件
2019/09/21 Javascript
基于Nuxt.js项目的服务端性能优化与错误检测(容错处理)
2019/10/23 Javascript
react quill中图片上传由默认转成base64改成上传到服务器的方法
2019/10/30 Javascript
Websocket 向指定用户发消息的方法
2020/01/09 Javascript
Python实现的监测服务器硬盘使用率脚本分享
2014/11/07 Python
python使用xlrd实现检索excel中某列含有指定字符串记录的方法
2015/05/09 Python
Python中返回字典键的值的values()方法使用
2015/05/22 Python
利用aardio给python编写图形界面
2017/08/21 Python
python中的for循环
2018/09/28 Python
djang常用查询SQL语句的使用代码
2019/02/15 Python
浅谈Python基础—判断和循环
2019/03/22 Python
django重新生成数据库中的某张表方法
2019/08/28 Python
Django配置MySQL数据库的完整步骤
2019/09/07 Python
Python使用matplotlib绘制三维参数曲线操作示例
2019/09/10 Python
tensorflow 实现自定义梯度反向传播代码
2020/02/10 Python
树莓派4B安装Tensorflow的方法步骤
2020/07/16 Python
python+pygame实现坦克大战小游戏的示例代码(可以自定义子弹速度)
2020/08/11 Python
运动鞋中的劳斯莱斯:索康尼(SAUCONY)
2017/08/09 全球购物
英国鹦鹉店:Parrot Essentials
2018/12/03 全球购物
房屋买卖协议书范本
2014/04/10 职场文书
酒店优秀员工事迹材料
2014/06/02 职场文书
中学团支部工作总结
2015/08/13 职场文书
MySQL查询日期时间
2022/05/15 MySQL