用执行时间在错误日志中为新手调试PHP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用执行时间在错误日志中为新手调试PHP相关的知识,希望对你有一定的参考价值。

I'm originally a sys admin and i love "tail - f /var/log/*" ... so i created a similar function in php that will allow me to view in real time what classes/functions/files/lines of my code is being run...

The usage is really simple, just set a global variable "debug" to true/false...
Note: The usage of this function will slow down your script... use it only to debug. Turn it off on production.

to view the logs, go to your default php error log file. Enjoy!
  1. // Do you want to debug?
  2. define('debug',true);
  3. $start_time = microtime(true);
  4.  
  5. // Include this function to your functions.inc.php or whatever shared libraries you are using.
  6. function debug($trace,$query = null){
  7. if(debug){
  8. $caller=array_shift($trace);
  9. if(isset($caller['class']))
  10. error_log("Initiating class: " . $caller['class']);
  11. if(isset($caller['function']))
  12. error_log("Calling function: " . $caller['function']);
  13. error_log("In file: " . $caller['file']);
  14. error_log("@ line: " .$caller['line']);
  15. if(isset($query)){
  16. error_log("Performing Query: " .$query);
  17. }
  18. error_log("---");
  19. }
  20. }
  21.  
  22. function shutdown() {
  23. global $start_time;
  24. error_log("Execution took: ".round((microtime(true) - $start_time),4)." seconds.");
  25. }
  26.  
  27. // Add the following lines in every function:
  28. if(debug){
  29. debug(debug_backtrace());
  30. }
  31.  
  32. // Examples:
  33. function clean($string,$type){
  34. if(debug){
  35. debug(debug_backtrace());
  36. }
  37. doTask();
  38. }
  39.  
  40. // You can also log all the queries you are using by using debug($query) like this:
  41. function selectQuery($query){
  42. if(debug){
  43. debug(debug_backtrace(),$query);
  44. }
  45. $q = mysql_query($query,$this->dblink) // Make sure $this->dblink goes to your MySQL Connection
  46. or die("MySQL Error: " . mysql_error());
  47. $result = mysql_fetch_assoc($q);
  48. return $result;
  49. }
  50.  
  51.  
  52. // Result looks like this:
  53. [12-Nov-2010 11:43:36] Initiating class: Db
  54. [12-Nov-2010 11:43:36] Calling function: dblink
  55. [12-Nov-2010 11:43:36] In file: /Users/sadus/Dropbox/Code/classes/db.class.php
  56. [12-Nov-2010 11:43:36] @ line: 8
  57. [12-Nov-2010 11:43:36] ---
  58. [12-Nov-2010 11:43:36] Initiating class: Db
  59. [12-Nov-2010 11:43:36] Calling function: selectQuery
  60. [12-Nov-2010 11:43:36] In file: /Users/sadus/Dropbox/Code/classes/usermanagement.class.php
  61. [12-Nov-2010 11:43:36] @ line: 76
  62. [12-Nov-2010 11:43:36] Performing Query: SELECT *
  63. FROM user
  64. WHERE email = '' AND verified = '1' LIMIT 1
  65. [12-Nov-2010 11:43:36] ---
  66. [12-Nov-2010 11:43:36] Execution took: 0.0076 seconds.

以上是关于用执行时间在错误日志中为新手调试PHP的主要内容,如果未能解决你的问题,请参考以下文章

PHP代码-psysh调试代码片段工具

运行/调试你的PHP代码

调试时“运行脚本”如何在 VS Code 中为 node.js 应用程序工作?

为啥 recyclerview$adapter 在片段中为空

片段视图返回后执行的 Firebase 查询

PHP调试日志方法