PHP中把Exception写入到日志
- 获取链接
- 电子邮件
- 其他应用
#wiki PHP5中把Exception写入到日志,是比较好的方法,开发时可以随时查看,部署后也不用担心在页面上打印出一对信息造成不友好的体验和安全隐患。代码:
<?php
error_reporting(E_ALL);
function exceptionLogger($exception) {
$file = 'exceptionLog.log';
file_put_contents($file,$exception->__toString(),FILE_APPEND);
echo "Sorry!I'm Sick...";
}
set_exception_handler('exceptionLogger');
function connectToDatabase() {
if(!$conn = @mysql_connect('localhost', 'root', '')) {
throw new Exception;
}
}
connectToDatabase();
?>
error_reporting(E_ALL);
function exceptionLogger($exception) {
$file = 'exceptionLog.log';
file_put_contents($file,$exception->__toString(),FILE_APPEND);
echo "Sorry!I'm Sick...";
}
set_exception_handler('exceptionLogger');
function connectToDatabase() {
if(!$conn = @mysql_connect('localhost', 'root', '')) {
throw new Exception;
}
}
connectToDatabase();
?>
评论