php 如何打印调试日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6079492/
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-25 23:17:39  来源:igfitidea点击:

How to print a debug log?

phpdebugginglogging

提问by eugene

I'd like to debug some PHP code, but I guess printing a log to screen or file is fine for me.

我想调试一些 PHP 代码,但我想将日志打印到屏幕或文件对我来说很好。

How should I print a log in PHP code?

我应该如何在 PHP 代码中打印日志?

The usual print/printfseems to go to HTML output not the console.

通常的print/printf似乎转到 HTML 输出而不是控制台。

I have Apache server executing the PHP code.

我有 Apache 服务器执行 PHP 代码。

回答by chx

A lesser known trick is that mod_php maps stderr to the Apache log. And, there is a stream for that, so file_put_contents('php://stderr', print_r($foo, TRUE))will nicely dump the value of $foointo the Apache error log.

一个鲜为人知的技巧是 mod_php 将 stderr 映射到 Apache 日志。并且,有一个流,因此file_put_contents('php://stderr', print_r($foo, TRUE))可以很好地将 的值转储$foo到 Apache 错误日志中。

回答by Olivier Berger

error_log(print_r($variable, TRUE)); 

might be useful

可能有用

回答by pthurlow

You can use error_logto send to your servers error log file (or an optional other file if you'd like)

您可以使用error_log将错误日志文件发送到您的服务器(如果您愿意,也可以使用其他可选文件)

回答by user3029507

If you are on Linux:

如果您使用的是 Linux:

file_put_contents('your_log_file', 'your_content');

or

或者

error_log ('your_content', 3, 'your_log_file');

and then in console

然后在控制台中

tail -f your_log_file

This will show continuously the last line put in the file.

这将连续显示文件中的最后一行。

回答by Sylverdrag

You need to change your frame of mind. You are writing PHP, not whatever else it is that you are used to write. Debugging in PHP is not done in a console environment.

你需要改变你的心态。您正在编写 PHP,而不是您习惯编写的任何其他内容。PHP 中的调试不是在控制台环境中完成的。

In PHP, you have 3 categories of debugging solutions:

在 PHP 中,您有 3 类调试解决方案:

  1. Output to a webpage (see dBug library for a nicer view of things).
  2. Write to a log file
  3. In session debugging with xDebug
  1. 输出到网页(请参阅 dBug 库以获得更好的视图)。
  2. 写入日志文件
  3. 使用 xDebug 进行会话调试

Learn to use those instead of trying to make PHP behave like whatever other language you are used to.

学习使用它们,而不是试图让 PHP 表现得像您习惯的任何其他语言。

回答by stefgosselin

Are you debugging on console? There are various options for debugging PHP. The most common function used for quick & dirty debugging is var_dump.

你是在控制台调试吗?调试 PHP 有多种选择。用于快速和脏调试的最常用函数是var_dump

That being said and out of the way, although var_dump is awesome and a lot of people do everything with just that, there are other tools and techniquesthat can spice it up a bit.

话虽如此,但尽管 var_dump 很棒,而且很多人都用它来做任何事情,但还有其他工具和技术可以让它更有趣。

Things to help out if debugging in a webpage, wrap <pre> </pre>tags around your dump statement to give you proper formatting on arrays and objects.

如果在网页中进行调试,可以<pre> </pre>在转储语句周围包装 标签以提供正确的数组和对象格式。

Ie:

IE:

<div> some html code ....
      <a href="<?php $tpl->link;?>">some link to test</a>
</div>

      dump $tpl like this:

    <pre><?php var_dump($tpl); ?></pre>

And, last but not least make sure if debugging your error handling is set to display errors. Adding this at the top of your script may be needed if you cannot access server configuration to do so.

而且,最后但并非最不重要的一点是确保调试您的错误处理是否设置为显示错误。如果您无法访问服务器配置,则可能需要将其添加到脚本顶部。

error_reporting(E_ALL);
ini_set('display_errors', '1');

Good luck!

祝你好运!

回答by jopke

If you don't want to integrate a framework like Zend, then you can use the trigger_errormethod to log to the php error log.

如果不想集成Zend 之类的框架,那么可以使用trigger_error方法记录到 php 错误日志中。

回答by Danzan

Simply way is trigger_error:

简单的方法是trigger_error:

 trigger_error("My error");

but you can't put arrays or Objects therefore use

但你不能把数组或对象因此使用

var_dump

回答by inorganik

You can also write to a file like this:

您还可以写入这样的文件:

$logFilePath = '../logs/debug.text';
ob_start();

// if you want to concatenate:
if (file_exists($logFilePath)) {
    include($logFilePath);
}
// for timestamp
$currentTime = date(DATE_RSS);

// echo log statement(s) here
echo "\n\n$currentTime - [log statement here]";

$logFile = fopen($logFilePath, 'w');
fwrite($logFile, ob_get_contents());
fclose($logFile);
ob_end_flush();

Make sure the proper permissions are set so php can access and write to the file (775).

确保设置了正确的权限,以便 php 可以访问和写入文件 ( 775)。

回答by steffanjj

You can use:

您可以使用:

<?php
echo '<script>console.log("debug log")</script>';
?>