PHP中把Exception写入到日志
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(); ?>
最后修改于 2009-07-26