好的,我有另一个问题 / a>对于我的日志阶级,但我希望能够将呼叫脚本的行号添加到日志文件条目中。

我已经看到 __line __但这给了我它的行号。

示例:

a.php

$log = new Logger();
$log->debug('hello'); // Say this is line #20
.

现在在Debug()中的Logger.php类中我使用 __线 __魔法常量在#300中。当我运行脚本时,我希望日志条目以读取“在线20”,但它在第300行中读取'。除了将行号传递到函数之外,我可以做到这一点吗?

示例调试类功能

public function debug($message) {
        if(DEBUG) {
            $this->calling_script = $this->getScriptBaseName();
            $this->log_file = LOG_FILE_DIRECTORY."/".$this->calling_script.".log";
            $this->fh = fopen($this->log_file, 'a') or die("Can't open log file: ".$this->log_file);

            if($this->first_run) {
                $this->log_entry = "\n[" . date("Y-m-d H:i:s", mktime()) . "][debug][line:".__LINE__."]:\t".$message."\n";
            } else {
                $this->log_entry = "[" . date("Y-m-d H:i:s", mktime()) . "][debug][line:".__LINE__."]:\t".$message."\n";
            }       
            fwrite($this->fh, $this->log_entry);
            fclose($this->fh);

            $this->first_run = false;
        }       
    }
.

编辑:debug_backtrace()工作很棒!!!工作在下面

public function debug($message) {
        if(DEBUG) {
            $debug_arr = debug_backtrace();
            $this->calling_script = $this->getScriptBaseName();
            $this->log_file = LOG_FILE_DIRECTORY."/".$this->calling_script.".log";
            $this->fh = fopen($this->log_file, 'a') or die("Can't open log file: ".$this->log_file);

            if($this->first_run) {
                $this->log_entry = "\n[" . date("Y-m-d H:i:s", mktime()) . "][debug]:\t".$message." [line:".$debug_arr[0]['line']."]\n";
            } else {
                $this->log_entry = "[" . date("Y-m-d H:i:s", mktime()) . "][debug]:\t".$message." [line:".$debug_arr[0]['line']."]\n";
            }       
            fwrite($this->fh, $this->log_entry);
            fclose($this->fh);

            $this->first_run = false;
        }       
    }
.

有帮助吗?

解决方案

您必须使用 debug_backtrace 为此或否则始终将线路(具有__LINE__)传递给函数。

其他提示

你见过 debug_backtrace()?我不确定它的开销。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top