PHP error_log() writes error information to a file (definition and usage)_PHP tutorial

WBOY
Release: 2016-07-13 10:25:59
Original
701 people have browsed it

In PHP programming, it is a good programming habit to develop the programming habit of writing log files. Programmers should learn this programming idea and not be too impetuous. Lack of rigor in early programming will often lead to difficulties in later maintenance and debugging, which will require more time and energy.
error_log() is a function that sends error information to a certain place. It is common in programming, especially in the program debugging stage.
This article will use examples to explain the usage of the error_log() function, as well as some issues that need attention.

Copy code The code is as follows:

$str='This is an error message. ';
error_log($str,3,'errors.log');
?>

The above is the most commonly used error_log() example. Its function is to convert an Information is written to the errors.log file, which is automatically created if it does not exist. In this example, we see that there is a parameter "3". Note that this number "3" cannot be changed or removed.
The following is a list of problems that may arise when using the error_log() function:
(1) Program error message: Warning: error_log() [function.error-log]: failed to open stream: Permission denied in ...on line ...
The above error occurs because the file does not have write permission. Just enable the file write permission in the directory.
(2) The information written to the log file cannot be line-wrapped
Use error_log() to write the log file. You will find that the text is not line-wrapped. You can make the following improvements to the above code:
Copy code The code is as follows:

$str="This is an error message. rn";
error_log ($str,3,'errors.log');
?>

Note that $str uses double quotes (the difference between single quotes and double quotes in PHP). rn is added to the end of the string. This is different from the first example.
The following is an introduction to the error_log() function
Format
bool error_log ( string $message [, int $message_type=0 [, string $destination [, string $extra_headers ]]] )
Send error messages to the web server's error log, or to a file.
message The error message that should be logged.
message_type
Sets where errors should be sent. Use the operating system's logging mechanism or a file, depending on what the error_log directive is set to. Possible message types are:
0 message sent to PHP's system log. This is the default option. Where is the log file generated by the error message generated by the IIS server when running and debugging the PHP program.
1 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 is no longer an option.
3 message is sent to the file at destination. The character message is not treated as a new line by default, but is appended to the end of the line.
4 messages are sent directly to the SAPI log handler.
destination target. Its meaning is described above and is determined by the message_type parameter.
extra_headers Extra headers. Used when message_type is set to 1. This message type uses the same built-in function of mail().

Return value TRUE on success, or FALSE on failure.

Another example

Copy the code The code is as follows:

Send an email with a custom error:
$test=2; if ($test>1) {
error_log("A custom error has been triggered", 1,"someone@example.com","From: webmaster@example.com");
}
?>

Output:
A custom error has been triggered

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824869.htmlTechArticleIn PHP programming, it is a good programming habit to develop the programming habit of writing log files. Programmers should learn this kind of programming thinking and not be too impetuous. The early programming was not rigorous...
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!