关于PHP中如何跟踪报错的手段

PHP中发生报错,按照道理可以开启报错提示来进行报错输出:


error_reporting();


ini_set('display_errors','on');


可以查看php.ini中的配置:


error_reporting = E_ALL 



display_errors = On


的开启情况。



另外,我们也可以通过开启php.ini中的php报错日志来查看,例如:


error_log = "d:/wamp/logs/php_error.log"


此时也许我们会发现我们的apache服务器无法启动了,其实这是很多人碰到的情况,一旦开启php.ini中的error_log日志,apache就无法启动。

我这里已经解决掉了这个问题,为了模拟,我们到php.5.3.5中的ext目录下随便把一个.dll文件剪切掉,例如这个php_dblib.dll,这个是php5.3之后连接mssql的扩展库,是微软提供的而不是php本身,因为php5.3之后已不是原生支持连接mssql,把他剪切掉之后,我们重启apache,就会发现apache已无法启动,这个时候我们去看apache的apache_error.log日志,会发现如下报错:


[Thu Jan 16 11:30:12 2014] [warn] pid file D:/wamp/bin/apache/Apache2.2.17/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?


网上很多人出了此报错不知道原因,其实是因为php5.3.5中,如果开启了error_log,那么一定要保证加载的.dll必须存在,否则将无法启动apache,不信我们可以此时关掉apache,然后到php.ini中将error_log前面加上分号注释掉,然后此时再重启启动apache,会发现apache的报错日志apache_error.log中报错如下:


[Thu Jan 16 11:30:12 2014] [warn] pid file D:/wamp/bin/apache/Apache2.2.17/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Thu Jan 16 11:38:46 2014] [warn] pid file D:/wamp/bin/apache/Apache2.2.17/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
PHP Warning:  PHP Startup: Unable to load dynamic library 'd:/wamp/bin/php/php5.3.5/ext/php_dblib.dll' - \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc4\xa3\xbf\xe9\xa1\xa3\r\n in Unknown on line 0
<br />
<b>Warning</b>:  PHP Startup: Unable to load dynamic library 'd:/wamp/bin/php/php5.3.5/ext/php_dblib.dll' - 找不到指定的模块。


 in <b>Unknown</b> on line <b>0</b><br />
[Thu Jan 16 11:38:46 2014] [notice] Apache/2.2.17 (Win32) PHP/5.3.8 configured -- resuming normal operations
[Thu Jan 16 11:38:46 2014] [notice] Server built: Oct 18 2010 01:58:12
[Thu Jan 16 11:38:47 2014] [notice] Parent: Created child process 10316
PHP Warning:  PHP Startup: Unable to load dynamic library 'd:/wamp/bin/php/php5.3.5/ext/php_dblib.dll' - \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc4\xa3\xbf\xe9\xa1\xa3\r\n in Unknown on line 0
<br />
<b>Warning</b>:  PHP Startup: Unable to load dynamic library 'd:/wamp/bin/php/php5.3.5/ext/php_dblib.dll' - 找不到指定的模块。


 in <b>Unknown</b> on line <b>0</b><br />
[Thu Jan 16 11:38:47 2014] [notice] Child 10316: Child process is running
[Thu Jan 16 11:38:47 2014] [notice] Child 10316: Acquired the start mutex.
[Thu Jan 16 11:38:47 2014] [notice] Child 10316: Starting 64 worker threads.
[Thu Jan 16 11:38:47 2014] [notice] Child 10316: Starting thread to listen on port 80.


说明确实没有正确加载php_mssql.dll,不信我们此时可以再把php.ini中的error_log开启,然后重启apache(此时无法启动apache),查看apache报错日志:


[Thu Jan 16 11:30:12 2014] [warn] pid file D:/wamp/bin/apache/Apache2.2.17/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Thu Jan 16 11:38:46 2014] [warn] pid file D:/wamp/bin/apache/Apache2.2.17/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
PHP Warning:  PHP Startup: Unable to load dynamic library 'd:/wamp/bin/php/php5.3.5/ext/php_dblib.dll' - \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc4\xa3\xbf\xe9\xa1\xa3\r\n in Unknown on line 0
<br />
<b>Warning</b>:  PHP Startup: Unable to load dynamic library 'd:/wamp/bin/php/php5.3.5/ext/php_dblib.dll' - 找不到指定的模块。


 in <b>Unknown</b> on line <b>0</b><br />
[Thu Jan 16 11:38:46 2014] [notice] Apache/2.2.17 (Win32) PHP/5.3.8 configured -- resuming normal operations
[Thu Jan 16 11:38:46 2014] [notice] Server built: Oct 18 2010 01:58:12
[Thu Jan 16 11:38:47 2014] [notice] Parent: Created child process 10316
PHP Warning:  PHP Startup: Unable to load dynamic library 'd:/wamp/bin/php/php5.3.5/ext/php_dblib.dll' - \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc4\xa3\xbf\xe9\xa1\xa3\r\n in Unknown on line 0
<br />
<b>Warning</b>:  PHP Startup: Unable to load dynamic library 'd:/wamp/bin/php/php5.3.5/ext/php_dblib.dll' - 找不到指定的模块。


 in <b>Unknown</b> on line <b>0</b><br />
[Thu Jan 16 11:38:47 2014] [notice] Child 10316: Child process is running
[Thu Jan 16 11:38:47 2014] [notice] Child 10316: Acquired the start mutex.
[Thu Jan 16 11:38:47 2014] [notice] Child 10316: Starting 64 worker threads.
[Thu Jan 16 11:38:47 2014] [notice] Child 10316: Starting thread to listen on port 80.
[Thu Jan 16 11:42:11 2014] [notice] Parent: Received shutdown signal -- Shutting down the server.
[Thu Jan 16 11:42:11 2014] [notice] Child 10316: Exit event signaled. Child process is ending.
[Thu Jan 16 11:42:12 2014] [notice] Child 10316: Released the start mutex
[Thu Jan 16 11:42:13 2014] [notice] Child 10316: All worker threads have exited.
[Thu Jan 16 11:42:13 2014] [notice] Child 10316: Child process is exiting
[Thu Jan 16 11:42:13 2014] [notice] Parent: Child process exited successfully.
[Thu Jan 16 11:42:31 2014] [warn] pid file D:/wamp/bin/apache/Apache2.2.17/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Thu Jan 16 11:42:38 2014] [warn] pid file D:/wamp/bin/apache/Apache2.2.17/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?


会发现又出现之前的报错了,对吧。印证了此php5.3.5版本中确实存在打开error_log之后需保证apache加载的dll都应存在的版本特性。


好,此特性我们告一段落,知道此特性之后,我们只需要在开启error_log之前查看apache报错日志,将没能正确加载的dll都在php.ini中注释掉,确保apache启动时没有此类报错,则可以使用php的error_log了。


因为我已经将php_dblib.dll从ext中去除了,所以我代码中有一段访问数据库的脚本按理说应该报错,但是此时我通过web访问发现页面空白,没有任何报错,另外php的php_error.log文件中也是一片空白,这是什么情况?@_@,难道会有php报错是无法扑捉到的吗?!!


其实php中的php_error.log应该可以记录所有php报错,但是也有例外,是什么呢?当我去检查代码的时候,才发现我的数据库驱动脚本中是这么写的:


function db_connect()
{
if ($this->port != '')
{
$this->hostname .= ','.$this->port;
}


return @mssql_connect($this->hostname, $this->username, $this->password);
}


大家注意了没有,这个@mssql_connect啊,万恶的@啊,他竟然把本该输出的报错给按回去了,就是这里啊,神啊~~~找了我好半天啊,不信我们这个时候再把@去掉,然后重新访问此脚本,发现页面报错,到php报错日志中查看,也发现以下报错:


[16-Jan-2014 04:03:07] PHP Fatal error:  Call to undefined function mssql_connect() in D:\wamp\www\monite\system\database\drivers\mssql\mssql_driver.php on line 63


[16-Jan-2014 04:03:07] PHP Stack trace:


[16-Jan-2014 04:03:07] PHP   1. {main}() D:\wamp\www\monite\index.php:0


[16-Jan-2014 04:03:07] PHP   2. require_once() D:\wamp\www\monite\index.php:202


[16-Jan-2014 04:03:07] PHP   3. Cron_promote->__construct() D:\wamp\www\monite\system\core\CodeIgniter.php:308


[16-Jan-2014 04:03:07] PHP   4. CI_Loader->model() D:\wamp\www\monite\application\controllers\cron_promote.php:17


[16-Jan-2014 04:03:07] PHP   5. Cron_assets_expense_model->__construct() D:\wamp\www\monite\system\core\Loader.php:321


[16-Jan-2014 04:03:07] PHP   6. CI_Loader->database() D:\wamp\www\monite\application\models\BI\cron_assets_expense_model.php:11


[16-Jan-2014 04:03:07] PHP   7. DB() D:\wamp\www\monite\system\core\Loader.php:366


[16-Jan-2014 04:03:07] PHP   8. CI_DB_driver->initialize() D:\wamp\www\monite\system\database\DB.php:153


[16-Jan-2014 04:03:07] PHP   9. CI_DB_mssql_driver->db_connect() D:\wamp\www\monite\system\database\DB_driver.php:123



万恶的@,其实,php_error.log在你不做干预的时候会记录所有报错,除非你把他@掉。当然也许php还有其他报错和阻止报错的手段,等以后发现再补充~~


好吧好吧,就这样了,大家自己玩吧~~有什么新发现可以继续记录,嘿嘿~~

你可能感兴趣的:(php报错,php_error.log)