error_log() function sends an error message to the server error log, a file, or a remote destination. Its syntax
error_log(message,type,destination,headers);
Parameters | Description |
---|---|
message | Required. Specifies the error message to be logged. |
type | Optional. Specifies where errors should be sent. Possible values:
|
destination | Optional. Specifies the destination of the error message. The value is determined by the value of the type parameter. |
headers | Optional. Specifies additional headers such as From, Cc, and Bcc. This message type uses the same built-in function of mail(). Only used when message_type is set to 1. CRLF (\r\n) should be used to separate multiple headers. |
#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 file to the file you need.
To illustrate the simple use of the 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:
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 The function can record where the wrong SQL is found, and then automatically record the time, current page, and wrong SQL statement information to the /tmp/php_sql_err.log file. Then, when we are debugging the program When we find that the data extraction is incorrect or there is no data extraction, 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:
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; }
We determine whether the SQL is wrong in it, If an error occurs, false is returned, and then we can check the log to see if our function runs as we expected.
We execute: tail /tmp/php_sql_err.log
You 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']
It is roughly because when we selected, we did not write the field names that need to be extracted, then we can check the news_list.php file, The getNewsContent () function method does not pass the $field parameter into it, resulting in an SQL execution error. Therefore, the error_log() function helps to check whether our SQL is written correctly, or the parameters are not passed correctly. This greatly reduces the development burden and allows us to conduct unit testing of our program.
Of course, you can also use the error_log() function to record more error logs to facilitate PHP development. This is all up to you.
Use it to record simple logs in this work
<code> $address = post('address'); $group_add = explode(',',$address); $url ="/usr/local/apache/eyoung/tmp/maillog_".date("Y-m-d").".log"; foreach($group_add as $value) { /** {{{ modify by muzhaoyang -2006.12.13- 记log * 已空格分隔数据,记录的数据为写文本时间 拉票人ID 选手ID 发信地址 */ $logstr=date("H:i:s")." ".$uid." ".$star_uid." ".$value."\n"; error_log($logstr,3,$url); //}}} } </code>
The above is the detailed content of Introduction to the use of php error_log() function. For more information, please follow other related articles on the PHP Chinese website!