PHP从日志记录事件获取行号

编程入门 行业动态 更新时间:2024-10-27 16:23:40
本文介绍了PHP从日志记录事件获取行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,我还有一个问题 HERE 作为我的日志记录类,但我希望能够将调用脚本的行号添加到日志文件条目中.

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.

我看过 __Line

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

示例:

a.php

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

现在在debug()的Logger.php类中,我使用 __线 __例如第300行的魔术常数.当我运行脚本时,我希望日志条目读为在线20",但它读为在线300".除了将行号传递给函数之外,还有其他方法可以做到这一点吗?

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?

示例调试类函数

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()效果很好!!!在下面工作

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

推荐答案

您将不得不使用 debug_backtrace 为此,否则始终将行(带有__LINE__)传递给函数.

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

更多推荐

PHP从日志记录事件获取行号

本文发布于:2023-11-15 17:34:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:行号   事件   日志   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!