0

我希望在每个函数的末尾调用一个自定义错误日志/调试函数。

例子:

  • 我想打电话error_log(__METHOD__);
  • 我想要echo $query;
  • 显示执行时间等。

在每个函数的末尾出于调试目的而不必每次都调用该自定义函数。

非常感激。

4

2 回答 2

2

为此使用 Debugger/Profiler,例如使用XDebug或 Zend Debugger。

两者的比较见

于 2010-10-20T11:31:12.457 回答
1

经过很多 RTM 我不得不使用debug_backtrace();即使它很贵。

我是这样做的:

// debug(debug_backtrace(),$query);
// $query is optional
define('DEBUG',true);

function debug($trace,$query = null) {
    if(DEBUG) {
        $caller=array_shift($trace);
        error_log("Initiating class: " . $caller['class']);
        error_log("Calling function: " . $caller['function']);
        error_log("In file: " . $caller['file']);
        error_log("@ line: " .$caller['line']);
        if(isset($query))
        {
            error_log("Performing Query: " .$query);
        }
        error_log("---");
    }
   else
       exit();
}

并在每个函数的末尾添加以下内容:

function init_userInfo($ai, $v) {
    $this->user[$ai] = $v;
    debug(debug_backtrace());
}

或者如果函数有 SQL 查询:

function insertQuery($query)
{
    mysql_query($query)
        or die("MySQL Error: " . mysql_error());
    debug(debug_backtrace(),$query);

}

php_error.log 中的输出通常是这样的:

[20-Oct-2010 19:02:07] Initiating class: Db
[20-Oct-2010 19:02:07] Calling function: selectQuery
[20-Oct-2010 19:02:07] In file: /Code/classes/user.class.php
[20-Oct-2010 19:02:07] @ line: 100
[20-Oct-2010 19:02:07] Performing Query: SELECT * FROM user WHERE uid=(3) LIMIT 1
[20-Oct-2010 19:02:07] ---
[20-Oct-2010 19:02:07] Initiating class: User
[20-Oct-2010 19:02:07] Calling function: htmlform_addUserInfo
[20-Oct-2010 19:02:07] In file: /Code/index.php
[20-Oct-2010 19:02:07] @ line: 6
[20-Oct-2010 19:02:07] ---
于 2010-10-20T16:07:42.550 回答