Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
863 views
in Technique[技术] by (71.8m points)

debugging - Problems tracing file and line for debug logging (PHP)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...