将调试内容输出到文件中的最简单方法

问题描述:

I've got an applicaiton with various php scripts. I want to get an idea of how much time it takes for the execution to move from one point to another (there are many such points).

I was looking for a simple php line that I can insert without modification, at different spots in my code and get an output file (NOT generated html) which shows something like:

FILENAME      FUNCTION        LINENUMBER      TIMESTAMP

I've started using this:

file_put_contents('/home/default/public_html/debug.log',  __FILE__ . "\t" .
 __FUNCTION__ . "\t" . __LINE__ . "\t" . microtime(true)."
", FILE_APPEND);

which is good, but are there better ways of doing the same?

我有一个应用各种PHP脚本的应用程序。 我想知道执行从一个点移动到另一个点需要多长时间(有很多这样的点)。 p>

我正在寻找一个简单的php行, 我可以在不修改的情况下插入我的代码中的不同位置并获取输出文件(未生成html),其中显示如下内容: p>

  FILENAME FUNCTION LINENUMBER TIMESTAMP 
  code  >  pre> 
 
 

我开始使用它: p>

  file_put_contents('/ home / default / public_html / debug.log',__ FILE__。  “\ t”。
 __FUNCTION__。“\ t”._ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  _  >哪个好,但是有更好的方法吗? p> 
  div>

For a simple way to do this, you can either use the global PHP error system with trigger_error() and a E_NOTICE level, or the error_log() function to log in a separate file.

I your case I would recommend the second solution.


With the trigger_error():

<?php
    trigger_error("Call will start now", E_NOTICE);
    startTheHugeCall();
    trigger_error("Call is finished", E_NOTICE);
?>

With the error_log() :

<?php
    error_log("Call will start now", 3, "/var/tmp/debugfile.log");
    startTheHugeCall();
    error_log("Call is finished", 3, "/var/tmp/debugfile.log");
?>

Resources :

You could use the XDebug extension to generate raw profiling data. It will generate files in your /tmp directory by default that you can crunch using command line tools or various GUIs.