在PHP中使用trigger_error時如何獲取正確的行號?

[英]How to get proper line number when using trigger_error in PHP?


I am using trigger_error to "throw" errors in a custom class. My problem is that trigger_error prints out the line number where trigger_error was called. For example, given the following code:

我正在使用trigger_error在自定義類中“拋出”錯誤。我的問題是trigger_error打印出調用trigger_error的行號。例如,給出以下代碼:

01    <?php
02        class Test {
03            function doAction() {
04                $this->doSubAction();
05            }
06            
07            function doSubAction() {
08                if(true)
09                    trigger_error('Custom error', E_USER_WARNING);
10            }
11        }
12        
13        $var = new Test();
14        $var->doAction();
15    ?>

PHP will print out the following:

PHP將打印出以下內容:

Warning: Custom error in test.php on line 9

警告:第9行的test.php中的自定義錯誤

How would you make PHP return the line where the doAction() function was called (the method called outside the class, ignoring all calls made internally) as follows?

你將如何使PHP返回調用doAction()函數的行(在類外部調用的方法,忽略內部進行的所有調用),如下所示?

Warning: Custom error in test.php on line 14

警告:第14行的test.php中的自定義錯誤

Edit: Modified my example to be something a bit closer to what I'm trying to achieve.

編輯:修改我的例子,使其更接近我正在努力實現的目標。

5 个解决方案

#1


2  

The best thing to do is set up an error handler that shows a full stack trace.

最好的辦法是設置一個顯示完整堆棧跟蹤的錯誤處理程序。

Set up a custom error handler, and call debug_print_backtrace() in it. You will find this useful in general, as well in this specific circumstance.

設置自定義錯誤處理程序,並在其中調用debug_print_backtrace()。在這種特定情況下,您會發現這一點非常有用。

#2


0  

Two options, neither one are particularly palatable:

兩種選擇,都不是特別適合:

  • Override Have test() provide __LINE__ in the arguments (eg. "test(__LINE__)", and give the argument to trigger_error). Set an error handler to print a custom error message. NOTE: This is horrifically ugly. :-)

    覆蓋讓test()在參數中提供__LINE__(例如“test(__ LINE__)”,並將參數提供給trigger_error)。設置錯誤處理程序以打印自定義錯誤消息。注意:這是可怕的丑陋。 :-)

  • Set up an error handler, and have it call and process the horrifically-large output of debug_backtrace(). This function can be useful when debugging... But is overkill for what you're trying to do. Please don't use this function as part of the regular operation of a system.

    設置一個錯誤處理程序,讓它調用並處理debug_backtrace()的可怕大量輸出。調試時此功能非常有用......但對於您正在嘗試執行的操作來說,這樣做太過分了。請不要將此功能用作系統常規操作的一部分。

Short answer: too hard, don't try. :-|

簡答:太難了,不要試試。 : - |

#3


0  

I usually put a trigger_error() there also. This ensure that I know exactly where it's called and exactly where the actual error occured. I then send all of the errors to myself in an email when in production. I know this isn't automatic, but at least you can trace what happened.

我通常也會在其中放置一個trigger_error()。這樣可以確保我確切地知道它的確切位置以及發生實際錯誤的確切位置。然后,我在生產中通過電子郵件將所有錯誤發送給自己。我知道這不是自動的,但至少你可以追蹤發生的事情。

#4


0  

Alright, for those of you who are interested in my final solution, I integrated the following piece of code in our framework which returns the correct line number in all the cases that we could of tested. We are using it in production.

好的,對於那些對我的最終解決方案感興趣的人,我在我們的框架中集成了以下代碼,在我們可以測試的所有情況下返回正確的行號。我們正在生產中使用它。

ErrorHandler class

It catches uncaught PHP exceptions, PHP errors and PEAR::Error s. You will need to modify it a bit has the code has some framework specific functions, but they shouldn't be to hard to track down. Enjoy!

它捕獲未捕獲的PHP異常,PHP錯誤和PEAR ::錯誤。如果代碼具有一些特定於框架的功能,您將需要稍微修改它,但它們不應該難以追蹤。請享用!

#5


0  

I thought I'd throw my two cents into the pot and discuss what I generally use, either as is or with small customizations, for PHP libraries that I build that are often used by other developers.

我以為我會把我的兩分錢扔到底池中,討論我通常使用的內容,無論是原樣還是小型自定義,我構建的PHP庫通常被其他開發人員使用。

I subdivide errors that may arise during execution of the program into two categories: those that are a result of erroneous programming and those that occur due to either user error or some outside factor. For the former, I use trigger_error in conjunction with E_USER_ERROR and the latter an Exception, specifically a package Exception that is then inherited by all other Exceptions in the library.

我將程序執行過程中可能出現的錯誤細分為兩類:錯誤編程導致的錯誤以及由於用戶錯誤或外部因素導致的錯誤。對於前者,我將trigger_error與E_USER_ERROR結合使用,后者使用Exception,特別是包異常,然后由庫中的所有其他異常繼承。

An example of a development error would be passing an integer in a parameter that is expected to be a string (pre V7) or accessing a method or property of a class that doesn't exist. (You can use your own developer imagination here.) Obviously another developer isn't going to care that the error was generated deep in the bowels of a __get or __set statement or some other magic construct, but rather, they'll want to know specifically where their mistake is. Let's face it...developers don't like to wade through a backtrace.

開發錯誤的一個示例是在參數中傳遞一個整數,該參數應該是一個字符串(在V7之前)或訪問一個不存在的類的方法或屬性。 (你可以在這里使用你自己的開發人員的想象力。)顯然,另一個開發人員並不關心錯誤是在__get或__set語句或其他一些魔法構造的內部深處產生的,而是他們想要知道特別是他們的錯誤。讓我們面對現實......開發人員不喜歡通過回溯。

So the method I employ to localize the error message with is simply:

因此,我用來本地化錯誤消息的方法很簡單:

function localize_error_msg($msg, $level) {
    $level = (int)$level;
    $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $level + 1)[$level];
    return $msg . " in " . $backtrace['file'] . " on line " . $backtrace['line'];
}

I pass in the backtrace level that depends on the construct used which is generally 1 or 2 and the rest is self explanatory.

我傳入的回溯級別取決於所使用的構造,通常為1或2,其余部分是自解釋的。

To prevent the repetitive sequence of "in file on line n", I add this error handler:

為了防止“在線上n文件”的重復序列,我添加了這個錯誤處理程序:

set_error_handler(function($errno, $errstr, $errfile, $errline) {
    if (preg_match('/on line \d+$/', $errstr) === 1)
        die($errstr);
    else return false;
}, E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);

Again, I like to keep it simple. One can argue that the localization of the error message should take place in the handler but then, you have to be able to pass in the depth, which is now an additional 2 levels deep inside the handler, and, well, it gets messy after that.

同樣,我喜歡保持簡單。有人可能會爭辯說錯誤消息的本地化應該在處理程序中進行,但是,你必須能夠傳遞深度,現在處理程序內部還有2個深度,而且,它之后會變得混亂那。

Happy Trails!


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/10/09/1e5cb862d19fc0201a78c12691e35af5.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com