跟踪文件和调试日志记录行时出现问题 (PHP)
因此,我正在尝试创建一种允许动态记录调试消息的方法,并且我想包含消息发生的文件名和行号。我的第一个倾向是将 debug_backtrace() 作为日志记录方法的参数之一,该方法返回一个包含当前文件名和行号的数组。
问题是,这仅给出了第一个名为(index.php)的文件的文件和行。然而,index.php 只是一个五行文件,它从包含文件中的类调用方法,因此无论如何,行和文件信息总是显示(index.php,第 5 行)并且毫无用处。
有没有一种方法可以获取当前行和文件,无论您在代码中的哪个位置?
添加
这是文件和行信息:
[2011-01-23 06:26:10] 信息: “请求不存在 控制器(测试)。”,文件: “/home/spotless/public_html/mymvc/index.php”, 行:5,请求:“/test”
这是完整的 index.php:
<?php
include_once('application/init.php');
lev_init::init();
?>
这是在 init.php 文件中使用 debug_backtrace() 进行的日志记录调用(第 37 行):
// if requested controller does not exist, log
lev_logging::message('Request made for non-existant controller ('.$requested_controller.').', debug_backtrace());
第二次更新
debug_backtrace 的 var_dump
数组(1) { [0]=>;数组(6) { ["文件"]=>; 字符串(42) “/home/spotless/public_html/mymvc/index.php” [“行”]=> int(5) ["函数"]==> 字符串(4)“init”[“class”]=>字符串(8) “lev_init”[“类型”]=>字符串(2)“::” [“args”]=>数组(0) { } } }
So I am trying to make a method that allows for logging debug messages on the fly, and I would like to include the file name and line number where the message occured. My first inclination was to give debug_backtrace() as one of the arguments for the logging method, which returns an array which contains the current file name and line number.
The problem is, this only gives the file and line of the very first file called (index.php). index.php is only a five line file that calls a method from a class in an included file however, so the line and file information always say (index.php, line 5) no matter what and are useless.
Is there a way to get the current line and file no matter where in the code you are?
Addition
Here is the file and line info:
[2011-01-23 06:26:10] Information:
"Request made for non-existant
controller (test).", File:
"/home/spotless/public_html/mymvc/index.php",
Line: 5, Request: "/test"
Here is the index.php in its entirety:
<?php
include_once('application/init.php');
lev_init::init();
?>
Here is the logging call using the debug_backtrace(), within the init.php file (line 37):
// if requested controller does not exist, log
lev_logging::message('Request made for non-existant controller ('.$requested_controller.').', debug_backtrace());
second update
var_dump of debug_backtrace
array(1) { [0]=> array(6) { ["file"]=>
string(42)
"/home/spotless/public_html/mymvc/index.php"
["line"]=> int(5) ["function"]=>
string(4) "init" ["class"]=> string(8)
"lev_init" ["type"]=> string(2) "::"
["args"]=> array(0) { } } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您处于全局上下文中,而不是函数,那么您显示的是正常行为。文件的包含不会反映在调用堆栈中 - 仅反映函数和方法的调用。
据我所知,没有办法构建“包含跟踪”,嵌套包含一行代码的列表在其中。这已在SO和IIRC上反复询问,从未找到解决方案。
If you are in the global context with this, not a function, then what you show is normal behaviour. The inclusion of files does not reflect in the call stack - only the calling of functions and methods.
As far as I know, there is no way to build an "include trace", a list of the nested includes a line of code is in. This has been asked repeatedly on SO, and IIRC, a solution was never found.
debug_backtrace
返回一个数组,因此执行var_export(debug_backtrace(), true)
即:
注意:
只需编辑堆栈跟踪的内容/位置即可。
?>
b_test 中的 debug_backtrace 将显示包含内容之前的所有内容。 a_test 中的那个不会显示 b_test 调用,因为它已返回......
debug_backtrace
returns an array so do avar_export(debug_backtrace(), true)
i.e.:
Note:
just and edit what what/where the stack trace matters.
?>
The debug_backtrace in b_test will show everything up to the include. the one in a_test won't show the b_test call since it has returned...