跟踪文件和调试日志记录行时出现问题 (PHP)

发布于 2024-10-14 11:57:05 字数 1187 浏览 3 评论 0原文

因此,我正在尝试创建一种允许动态记录调试消息的方法,并且我想包含消息发生的文件名和行号。我的第一个倾向是将 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 技术交流群。

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

发布评论

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

评论(2

不语却知心 2024-10-21 11:57:06

如果您处于全局上下文中,而不是函数,那么您显示的是正常行为。文件的包含不会反映在调用堆栈中 - 仅反映函数和方法的调用。

据我所知,没有办法构建“包含跟踪”,嵌套包含一行代码的列表在其中。这已在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.

凝望流年 2024-10-21 11:57:06

debug_backtrace 返回一个数组,因此执行 var_export(debug_backtrace(), true)

即:

// if requested controller does not exist, log
lev_logging::message('Request made for non-existant controller ('.$requested_controller.').', var_export(debug_backtrace(), true));

注意:

只需编辑堆栈跟踪的内容/位置即可。

<?php
// filename: /tmp/a.php

function b_test($foo)
{
   var_dump(debug_backtrace());
}

function a_test($str)
{
    echo "\nHi: $str";
    b_test('bar');
    var_dump(debug_backtrace());
}

a_test('friend');
?>

<?php
// filename: /tmp/b.php
include_once '/tmp/a.php';

?>

b_test 中的 debug_backtrace 将显示包含内容之前的所有内容。 a_test 中的那个不会显示 b_test 调用,因为它已返回......

debug_backtrace returns an array so do a var_export(debug_backtrace(), true)

i.e.:

// if requested controller does not exist, log
lev_logging::message('Request made for non-existant controller ('.$requested_controller.').', var_export(debug_backtrace(), true));

Note:

just and edit what what/where the stack trace matters.

<?php
// filename: /tmp/a.php

function b_test($foo)
{
   var_dump(debug_backtrace());
}

function a_test($str)
{
    echo "\nHi: $str";
    b_test('bar');
    var_dump(debug_backtrace());
}

a_test('friend');
?>

<?php
// filename: /tmp/b.php
include_once '/tmp/a.php';

?>

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...

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