PHP error_log () function to process error logs

Source: Internet
Author: User
Tags types of functions

In

The PHP error_log () function is mainly used to write error logs. It is developed by many people or in a development project that is complex and has no unit test, you can use it to record errors in our program, especially errors in the execution of database query statements.

Let's take a rough look at the PHP error_log () function. Let's take a look at the manual explanation:

Error_log
(PHP 3, PHP 4, PHP 5)

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

The error log that sends an error message to the Web server, 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, and the third parameter is the target file, the fourth parameter is other header information. In fact, the fourth parameter is generally not used. We mainly look at the first three parameters. The second parameter is the message type, which includes four types: 0, 1, 2, and 3, 0 is the default type. Four types of functions:

0
The information content is sent to the PHP system log record. The operating system's own logging mechanism or a file depends on the configuration option error_log in php. ini. 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 message for sending an email. The second parameter is sent using the mail () function.

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

3
The message is appended to a target file as a new line. In fact, for the sake of convenience, it is more appropriate to directly use type 3 to write the log file to the file you need.

For the simple use of PHP error_log () function, an example is provided. Assume that our database abstract class uses the PEAR: DB class. Now I want to record whether our program has an execution error in the program. Then we use error_log () to record the SQL statement execution errors or failures. At least our PEAR: DB class provides DB: isError () method To obtain whether an error exists in an execution result object. Then, we can determine whether an error has occurred while executing an SQL statement and check whether logs are logged. At the same time, the object has a userinfo attribute, this attribute records the wrong SQL statement, so we can construct such a function:

 
 
  1. function logError($object)   
  2. {  
  3. if(DB::isError($object))   
  4. {  
  5. error_log(date("[Y-m-d H:i:s]")
    ." -[".$_SERVER['REQUEST_URI'].
    "] :".$object -> userinfo."n", 
    3, "/tmp/php_sql_err.log");  
  6. return true;  
  7. }  
  8. return false;  

The PHP error_log () function is used to record the error SQL, so the time, current page, and error SQL statement information are automatically recorded in the/tmp/php_ SQL _err.log file, so when we debug the program and find that the data extraction is incorrect or there is no data extraction, you can view the/tmp/php_ SQL _err.log file to view the error page and the error SQL statement.

Of course, we must use this function in our SQL query execution program. For example, we need to compile a function to extract news information:

 
 
  1. function getNewsContent($news_id, $field="")  
  2. {  
  3. global $db;  
  4. $result = $db->getRow("SELECT 
    $field FROM news WHERE news_id = '$news_id'");  
  5. if (logError($result))  
  6. {  
  7. return false;  
  8. }  
  9. return $result;  

Check whether the SQL statement is correct. If the SQL statement is incorrect, false is returned. Then, check the log to see if our PHP error_log () function runs as expected.

Run tail/tmp/php_ SQL _err.log to view information similar to this:

[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']

This is roughly because we did not write the field names to be extracted during the select operation, so we can check the news_list.php file and reduce the number of getNewsContent () functions to pass the $ field parameter to none, this causes an SQL Execution error. Therefore, the error_log () function helps check whether our SQL statements are correctly written, or if the parameters are not passed correctly, which greatly reduces the development burden, I thought about it, but I did a unit test on our program.

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.