PHP error_log () writes error messages to a file (Definition and usage)

Source: Internet
Author: User

It is a good programming habit to develop the programming habit of writing log files in php programming. programmers should learn this kind of programming idea and should not be too impetuous. Non-rigorous preliminary programming will often lead to difficulties in maintenance and adjustment in the future, and more time and effort will be paid.
Error_log () is a function that sends error messages to a specific place. It is common in programming, especially in the program debugging stage.
This article describes how to use the error_log () function and some precautions.
Copy codeThe Code is as follows:
<? Php
$ Str = 'This is an error message. ';
Error_log ($ str, 3, 'errors. log ');
?>

The above is the most common error_log () example. It is used to write a piece of information into the errors. log file. If the file does not exist, it is automatically created. In this example, we can see that there is a parameter "3". Note that this number "3" cannot be changed or removed.
The following lists the problems that may occur when using the error_log () function:
(1) program error prompt: Warning: error_log () [function. error-log]: failed to open stream: Permission denied in... on line...
The preceding error occurs because the file has no write permission. Enable the file write permission for this directory.
(2) The information written into the log file cannot be wrapped in line breaks.
If you use error_log () to write a log file, no line breaks are found in the text. You can make the following improvements to the above Code:
Copy codeThe Code is as follows:
<? Php
$ Str = "this is an error message. \ R \ n ";
Error_log ($ str, 3, 'errors. log ');
?>

Note that $ str is enclosed in double quotation marks (the difference between Single and Double quotation marks in php), and \ r \ n is added at the end of the string. This is different from the first example.
The following describes the error_log () function.
Format
Bool error_log (string $ message [, int $ message_type = 0 [, string $ destination [, string $ extra_headers])
Send the error message to the error log of the web server or to a file.
Error message to be recorded.
Message_type
Set where the error should be sent. The use of the operating system's log mechanism or a file depends on what is set by the error_log command. Possible information types include:
0 message: the system log sent to PHP. This is a default option. Where is the log file generated by the error message of the php program running and debugging on the iis server.
1. message is sent to the email address set by destination. The fourth parameter extra_headers is used only in this type.
2 is no longer an option.
3. The message is sent to the file at the destination location. The character message is not regarded as a new row by default, but appended to the end of the row.
4. message is directly sent to the SAPI log handler.
Destination target. Its meaning is described above and determined by the message_type parameter.
Extra_headers. It is used when message_type is set to 1. This information type uses the same built-in function of mail.

Returns TRUE if the return value is successful, or FALSE if the return value fails.

Another example
Copy codeThe Code is as follows:
Send an email with a custom error:
<? Php
$ 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

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.