PHP 错误与异常 笔记与总结(14 )记录和发送异常信息

当发生异常时,把异常信息记录到日志文件中:

 1 <?php
 2 header('content-type:text/html; charset=utf-8');
 3 class LogException extends Exception{
 4     public function __construct($message = null, $code = 0){
 5         parent::__construct($message, $code);
 6         error_log($this->getMessage().PHP_EOL, 3, 'D:/practise/php/Error/exceprion/testLogException.log');
 7     }
 8 }
 9 
10 try{
11     $conn = @mysql_connect('localhost', 'root', 'root123');
12     if(!$conn) throw new LogException("mysql connect failed", 1);
13 }catch(LogException $e){
14     echo $e->getMessage();
15 }

这里数据库密码错误,执行文件,输出:

 

testLogException.log文件:

 

line:6 处也可以记录下异常追踪信息 $this->getTraceAsString()

 

posted @ 2015-07-04 22:05  nemo20  阅读(203)  评论(0编辑  收藏  举报
访客数:AmazingCounters.com
2016/05/17 起统计