PHP執行過程中調試Log_PHP教程

來源:互聯網
上載者:User
1. function Dmess($msg, $prefix='') {

2. $debugMethod = 1;// 0-> print out stnd; 1-> write log file;

3. $traceArr = debug_backtrace();

4. $lastTrace = end($traceArr);

5. if( strlen($prefix)>0 )

6. $line = "[".date("Y-m-d H:i:s")."][".$prefix."] ".$msg;

7. else {

8. $line = "[".date("Y-m-d H:i:s")."][";

9. for($i=count($traceArr);$i>0;$i--){

10. $trace = $traceArr[$i-1];

11. $line .= basename($trace['file']).":".$trace['line'];

12. if( $i==count($traceArr) )

13. $line .= "|".$trace['function'];

14. $line .= "=>";

15. }

16. $line = substr($line, 0, -2);

17. $line .= "] " . $msg;

18. }

19. if( $debugMethod==0 ) {

20. print "\n\n";

21. //print "\nDEBUG:: $msg
\n";

22. flush();

23. ob_flush();

24. }

25. else {

26. $logfile = "./logs/debug.log";

27. write2file($line."\n", $logfile);

28. }

29. }

30.

31. function write2file($msg,$fileName)

32. {

33. $fp = @fopen($fileName, 'a');

34. if($fp==false)

35. {

36. $fp=@fopen($fileName,'w+');

37. if($fp==false)

38. {

39. if($fileName!=DEBUGFILE) {

40. echo "ERROR:: file ".$fileName." can not be created for writing";

41. exit();

42. }

43. else

44. {

45. echo " file ".$fileName." can not be created for writing";

46. return 0;

47. }

48. }

49. }

50. $lfp = flock($fp,LOCK_EX);

51. $i = 0;

52. while ($lfp == False)

53. {

54. $i++;

55. $lfp = flock($fp,LOCK_EX);

56. if ($i==10){return(0);}

57. }

58. fputs($fp,$msg);

59. flock($fp,LOCK_UN);

60. fclose($fp);

61. return 1;

62. }

摘自 氣度非等閑blog

http://www.bkjia.com/PHPjc/478559.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478559.htmlTechArticle1. function Dmess($msg, $prefix=) { 2. $debugMethod = 1;// 0- print out stnd; 1- write log file; 3. $traceArr = debug_backtrace(); 4. $lastTrace = end($traceArr); 5. if( strlen($pr...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.