PHP从记录事件中获取行号

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP从记录事件中获取行号脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,我的LOGging Class还有另一个问题 @L_301_0@,但我希望能够将调用脚本的行号添加到日志文件条目中.

我见过__Line__但是这给了我这行所在行的行号.

例:

a.PHP只会

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

现在在debug()的Logger.PHP类中,我使用了__Line__ Magic Constant,例如第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,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;
        }       
    }
@H_777_22@ 您必须为此使用 debug_backtrace,否则始终将该行(使用__LINE__)传递给该函数.

脚本宝典总结

以上是脚本宝典为你收集整理的PHP从记录事件中获取行号全部内容,希望文章能够帮你解决PHP从记录事件中获取行号所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。