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();
?>

评论

此博客中的热门博文

[转贴]江西潘阳陈振翊报警被警方击毙事件始末

龙脊梯田景区

你的妈妈已经等了二十几年