PHP 从日志事件中获取行号

发布于 2024-09-09 00:50:16 字数 2688 浏览 7 评论 0原文

好的,我还有另一个问题这里对于我的日志记录类,但我希望能够将调用脚本的行号添加到日志文件条目中。

我见过 但这给了我它所在行的行号。

示例:

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;
        }       
    }

Ok I have another question HERE for my Logging Class but I wanted to be able to add the line number of the calling script to the log file entry.

I have seen Line but this gives me the line number of the line where this is at.

Example:

a.php

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

Now in my Logger.php class in the debug() I use the Line Magic Constant on for example line #300. When I run the script I would like the log entry to read 'on line 20' but it read 'on line 300'. Besides passing the line number to the function is there any other way I could do this?

Example debug class function

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;
        }       
    }

EDIT: debug_backtrace() works great!!! Working below

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;
        }       
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌化 2024-09-16 00:50:16

您必须使用 debug_backtrace 为此,否则始终将行(使用 __LINE__)传递给函数。

You'll have to use debug_backtrace for this or else always pass the line (with __LINE__) to the function.

长亭外,古道边 2024-09-16 00:50:16

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

Have you seen debug_backtrace()? I'm not sure of its overhead though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文