跟踪文件和调试日志记录的问题(PHP) [英] Problems tracing file and line for debug logging (PHP)

查看:228
本文介绍了跟踪文件和调试日志记录的问题(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文件(第37行)中使用debug_backtrace()的日志记录调用,

  //如果请求的控制器不存在,则记录
lev_logging :: message('现存控制器('。$ requested_controller。')。',debug_backtrace());

第二次更新



var_dump的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){}}}



解决方案

debug_backtrace 返回一个数组,所以做一个 var_export(debug_backtrace() ,true)



ie:

  //如果请求的控制器不存在,则记录
lev_logging :: message('请求为不存在的控制器('。$ requested_controller。')',var_export(debug_backtrace(),true));

注意:



只需编辑什么堆栈跟踪的重要性在哪里?

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

函数b_test($ foo)
{
var_dump(debug_backtrace());
}

函数a_test($ str)
{
echo\\\
Hi:$ 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调用,因为它已经返回...


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) { } } }

解决方案

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

这篇关于跟踪文件和调试日志记录的问题(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆