PHP write Error Log

Source: Internet
Author: User
Tags php write types of functions

Error_log function is a built-in function in PHP. It 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 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.

Me
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 header information. Generally, the fourth Parameter
The number is 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 row.

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.

Is
The simple use of the example error_log () function is described in this example. Assume that the abstract class of our database uses the pear: DB class. Now I want to record whether our program has been executed.
Error. 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:

Function logerror ($ object)
{
If (DB: iserror ($ object ))
{
Error_log (date ("[Y-m-d h: I: S]"). "-[". $ _ server ['request _ URI ']. "]:". $ object-> userinfo. "/N", 3, "/tmp/php_ SQL _err.log ");
Return true;
}
Return false;
}

This function is the place where an error SQL statement is found. The time, current page, and incorrect SQL statement information are automatically recorded in/tmp/php_ SQL _err.log.
In the file, 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:

Function getnewscontent ($ news_id, $ field = "")
{
Global $ dB;
$ Result = $ db-> getrow ("select $ field from news where news_id = '$ news_id '");
If (logerror ($ result ))
{
Return false;
}
Return $ result;
}

Check whether the SQL statement is correct. If the SQL statement is incorrect, return false. Then, you can check the log to see if our function runs as expected.
Run the following command: tail/tmp/php_ SQL _err.log.
The following information is displayed:

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

Large
This is because the field names to be extracted are not written in the SELECT statement, so we can check the news_list.php file to reduce production costs.
The getnewscontent () function method does not pass the $ Field Parameter in, causing an SQL Execution error. So the error_log () function will help check our SQL writes.
Is it correct, or the parameter is not passed correctly, which greatly reduces the development burden, but wants to perform a unit test on our program.

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

By heiyeluren

 

From http://blog.csdn.net/heiyeshuwu/archive/2006/01/13/577838.aspx

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.