Home > Backend Development > PHP Tutorial > PHP error_log() function handles error logs_PHP tutorial

PHP error_log() function handles error logs_PHP tutorial

WBOY
Release: 2016-07-15 13:32:55
Original
952 people have browsed it

In

PHP error_log() function is mainly used to write error logs. We are developing with multiple people, or in complex situations without unit testing. In development projects, we can use it to record errors in our programs, especially errors in the execution of database query statements.

Let’s take an overview of the PHP error_log() function. Let’s take a look at the manual’s explanation:

error_log
(PHP 3, PHP 4, PHP 5)

bool error_log ( string message [, int message_type [, string destination [, string extra_headers ]]] )

Send an error message to the web server's error log, a TCP port or a file.

Let’s look at the parameters of the PHP error_log() function. The first parameter message is the message content, the second parameter type is the message type, the third parameter is the target file, and the fourth parameter is other headers. information. In fact, generally the fourth parameter is not used. We mainly look at the first three parameters. The second parameter is the message type, including four types: 0, 1, 2, 3. 0 is the default type. Four types of functions:

0
Information content is sent to PHP's system logging, using the operating system's own logging mechanism or a file, depending on the configuration option error_log in php.ini content. This is the default option.

1
Send the information content to an email address. The third parameter is an email address. The fourth parameter is a header information to send the email. The second type uses the mail() function. Send an email

2
The message is written remotely to a PHP debug server through the PHP debug server. Of course, the --enable-debugger must be turned on when PHP is compiled. In addition, the entire type Only valid for PHP 3

3
message is appended as a new line in a target file. In fact, for us, for the sake of simplicity, it is generally more appropriate to use type 3 directly for logging, and write the log files to the files we need.

To illustrate the simple use of the PHP error_log() function, let’s take an example. Assume that our database abstract class uses the PEAR::DB class. Now I want to record whether our program has execution errors in the program. Then we use error_log() to record the execution errors or failures of our SQL statements. At least our PEAR::DB class provides the DB::isError() method to get whether there is an error in an execution result object, then we can Determine whether an error occurred while executing a certain SQL statement, and then consider whether to record the log. At the same time, the object has a userinfo attribute, which records the wrong SQL statement. Then we can construct such a function:

<ol class="dp-xml">
<li class="alt"><span><span>function logError($object)   </span></span></li>
<li><span>{  </span></li>
<li class="alt"><span>if(DB::isError($object))   </span></li>
<li><span>{  </span></li>
<li class="alt">
<span>error_log(date("[Y-m-d H:i:s]")<br>." -[".$_SERVER['REQUEST_URI'].<br>"] :".$object -</span><span class="tag">></span><span> userinfo."n", <br>3, "/tmp/php_sql_err.log");  </span>
</li>
<li><span>return true;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>return false;  </span></li>
<li class="alt"><span>} </span></li>
</ol>
Copy after login

The PHP error_log() function can record the place where the error SQL is found, and then automatically record the time, current page, and error SQL statement information to the /tmp/php_sql_err.log file, then, When we are debugging the program and find that the data extraction is incorrect or there is no data extraction, then we can view the /tmp/php_sql_err.log file to view our error page and wrong SQL statement.

Of course, we must use this function in the program where we execute SQL queries. For example, if we write a function to extract news information:

<ol class="dp-xml">
<li class="alt"><span><span>function getNewsContent($news_id, $</span><span class="attribute">field</span><span>=</span><span class="attribute-value">""</span><span>)  </span></span></li>
<li><span>{  </span></li>
<li class="alt"><span>global $db;  </span></li>
<li>
<span>$</span><span class="attribute">result</span><span> = $db-</span><span class="tag">></span><span>getRow("SELECT <br>$field FROM news WHERE </span><span class="attribute">news_id</span><span> = </span><span class="attribute-value">'$news_id'</span><span>");  </span>
</li>
<li class="alt"><span>if (logError($result))  </span></li>
<li><span>{  </span></li>
<li class="alt"><span>return false;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>return $result;  </span></li>
<li><span>} </span></li>
</ol>
Copy after login

We determine whether the SQL is wrong and return false if it is wrong. Then we can check the log to see if our PHP error_log() function runs as we expected.

We execute: tail /tmp/php_sql_err.log, and we can see information similar to this:

[2006-01-12 11:44:34] -[/news_list.php ?news_id=1] :SELECT FROM news WHERE news_id = '1' [nativecode=1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version fo
r the right syntax to use near 'FROM news WHERE news_id = '1']

Probably because when we selected, we did not write in the field names that need to be extracted, then we can check the news_list.php file and reduce the getNewsContent() function The $field parameter is not passed in the normal direction, resulting in an SQL execution error. Therefore, the error_log() function helps check whether our SQL is written correctly, or whether the parameters are not passed correctly. This greatly reduces the development burden and allows us to unit test our program.

Of course, you can also use the PHP error_log() function to record more error logs to facilitate PHP development. This is all up to you.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446109.htmlTechArticleThe PHP error_log() function is mainly used to write error logs. We are developing with multiple people, or in In development projects that are relatively complex and do not have unit tests, you can use it to...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template