Introduction to the use of php error_log() function

怪我咯
Release: 2023-03-13 12:00:01
Original
1535 people have browsed it

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);
Copy after login
ParametersDescription
messageRequired. Specifies the error message to be logged.
type

Optional. Specifies where errors should be sent. Possible values:

  • 0 - Default. Messages are sent to PHP's system log, using the operating system's logging mechanism or a file, depending on the error_log directive in php.ini.

  • 1 - The message is sent to the email address set by the parameter destination. The fourth parameter extra_headers will only be used in this type.

  • 2 - Deprecated (only used in PHP 3)

  • 3 - Message is sent to location destination file. The characters message are not treated as a new line by default.

  • 4 - The message is sent directly to the SAPI log handler.

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;
}
Copy after login

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;
}
Copy after login

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(&#39;address&#39;);
            $group_add  = explode(&#39;,&#39;,$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>
Copy after login


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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!