How to print a debug log?

2019-03-07 11:42发布

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

How should I print a log in PHP code?

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

I have Apache server executing the PHP code.

15条回答
萌系小妹纸
2楼-- · 2019-03-07 11:56

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.

In PHP, you have 3 categories of debugging solutions:

  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

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

查看更多
【Aperson】
3楼-- · 2019-03-07 12:03

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

查看更多
爷的心禁止访问
4楼-- · 2019-03-07 12:03

You can use

<?php
{
    AddLog("anypage.php","reason",ERR_ERROR);
}
?>

or if you want to print that statement in an log you can use

AddLog("anypage.php","string: ".$string,ERR_DEBUG_LOW);
查看更多
狗以群分
5楼-- · 2019-03-07 12:04

Simply way is trigger_error:

 trigger_error("My error");

but you can't put arrays or Objects therefore use

var_dump
查看更多
Summer. ? 凉城
6楼-- · 2019-03-07 12:04

You can use:

<?php
echo '<script>console.log("debug log")</script>';
?>
查看更多
Melony?
7楼-- · 2019-03-07 12:08

If you are on 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.

查看更多
登录 后发表回答