如何将 PHP 回溯保存到错误日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8369275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:32:47  来源:igfitidea点击:

How can I save a PHP backtrace to the error log?

phperror-handlingbacktrace

提问by Leo Jiang

I'm using this right now:

我现在正在使用这个:

error_log(serialize(debug_backtrace()));

But I have to unserialize it every time. Is there a better way to store backtraces?

但我每次都必须反序列化它。有没有更好的方法来存储回溯?

回答by álvaro González

This should generate a readable string:

这应该生成一个可读的字符串:

error_log(print_r(debug_backtrace(), true));

Additionally, debug_print_backtrace()prints the back trace as string and its output can be captured with regular output buffer functions:

此外,debug_print_backtrace()将回溯打印为字符串,并且可以使用常规输出缓冲区函数捕获其输出:

ob_start();
debug_print_backtrace();
error_log(ob_get_clean());

回答by Igor Sydorenko

From my perspective the best approach is using an exception functionality:

从我的角度来看,最好的方法是使用异常功能:

$e = new Exception();
$e->getTraceAsString();

回答by Pramendra Gupta

    $log = var_export(debug_backtrace(), true);

Then use the variable $logto log in file or what ever.

然后使用该变量$log登录文件或其他任何内容。

回答by Kzqai

A little ugly but workable, I do this:

有点丑但可行,我这样做:

 error_log('Identifying string so that it doesn\'t just end up as gibberish' . json_encode(debug_backtrace()));

回答by Robert Sinclair

The following can either be written to a .txt file or you can also access it's contents (like $content[0]) as opposed to var_export which is a bit trickier I find:

以下内容可以写入 .txt 文件,也可以访问它的内容(如 $content[0]),而不是 var_export,我发现它有点棘手:

    $content = unserialize(serialize(debug_backtrace()));