序言
基础
配置
架构
路由
控制器
模板
调试
错误处理
网页调试
文件调试
安全
工具
部署
错误处理
LMLPHP设计之初,对程序安全和用户体验特别重视。创建项目后,LMLPHP会屏蔽程序报错,将Notice,Error,Exception,Fatal Error等级别的错误全部捕捉,写入日志文件。因此,在开发过程要学会查看日志文件特别重要。日志内容的格式为:"[日期] 访问路径, 错误日志内容"。文件名以log_开头加上当天日期,后缀名.txt,当日志超过2M后会自动重命名,在日期尾部加上时间戳。下面将举例进行描述:

Notice错误捕捉:
<?php

//...
//after app run
unset($testvar);
echo 
$testvar;
$arr=array(1);
echo 
$arr[2];
//...
?>
上面的代码由于写法上不严谨,会导致Notice错误。运行后会生成日志文件。日志内容示例如下:
[ 2014-09-10T23:23:58+08:00 ] /start/test, LMLPHP Notice:[8]Undefined variable: testvar in D:\PHP\code\lmlphp\lmlphp\module\ModuleStart.php line 12
[ 2014-09-10T23:23:58+08:00 ] /start/test, LMLPHP Notice:[8]Undefined offset: 2 in D:\PHP\code\lmlphp\lmlphp\module\ModuleStart.php line 14

Exception错误捕捉:
<?php

//...
//after app run
$router=array('/^\/$/'=> array(
    
'callback'=>array('undeclaredFunction')
));
lml()->app()->addRouter($router)->run();
//...
?>
上面的代码在添加路由时,定义的callback方法系未声明的方法,因此会触发系统的异常处理机制。上面的路由匹配URL为/时,调用undeclaredFunction。运行后会将异常信息写入日志,日志内容示例如下:
[ 2014-09-10T23:43:14+08:00 ] /, exception 'LmlException' with message 'Function:undeclaredFunction not exists' in D:\PHP\code\lmlphp\lml.min.php:1
Stack trace:
#0 D:\PHP\code\lmlphp\lml.min.php(1): LmlApp->callUserFunc(Array)
#1 D:\PHP\code\lmlphp\index.php(63): LmlApp->run()
#2 {main}

Fatal Error错误捕捉:
<?php

//...
//after app run
new UndeclaredClass();
//...
?>
上面的代码由于new了未声明的类,将会导致Fatal Error,运行后错误信息会写入日志文件,日志内容示例如下:
[ 2014-09-10T23:34:01+08:00 ] /start/test, LMLPHP Fatal Error:Class 'UndeclaredClass' not found in D:\PHP\code\lmlphp\lmlphp\module\ModuleStart.php line 15
在项目开发过程中,如果错误信息直接抛给用户,会带给用户不好的用户体验。因此,养成从日志分析问题的习惯是很重要的。
© 2024 LMLPHP 关于我们 联系我们 友情链接 耗时0.005774(s)
2024-04-19 07:45:56 1713483956