PHP error_log () writes error messages to a file (definition and usage) _php tips

Source: Internet
Author: User
Tags system log

In PHP program writing, to develop the writing log file programming habits, is a good programming habit, programmers should learn this programming ideas, not too impetuous. Pre-programming is not rigorous, often bring late maintenance and mode of difficulty, pay more time and energy will be.
Error_log () is a function that sends an error message to a place, which is more common in program programming, especially in the debugging phase of the program.
This article will use an example to explain the use of the Error_log () and some issues to be noted.

Copy Code code as follows:

<?php
$str = ' This is an error message. ';
Error_log ($STR, 3, ' Errors.log ');
?>

This is the most commonly used error_log () example, which is to write a piece of information into the Errors.log file, which is automatically created if it does not exist. In this example, we see a parameter "3", noting that the number "3" cannot be changed or removed.
Here's a list of possible problems with the Error_log () function:
(1) The error of the program prompts: Warning:error_log () [Function.error-log]: failed to open stream:permission denied ... on ...
The above error occurs because the file does not have write permissions and opens the file write permission for the directory.
(2) The information written to the log file cannot be wrapped.
Using Error_log () to write to the log file, you will find that the text is not wrapped, you can make the following improvements to the code:
Copy Code code as follows:

<?php
$str = "This is an error message. \ r \ n ";
Error_log ($STR, 3, ' Errors.log ');
?>

Note that the $STR is in double quotes (the difference between PHP single and double quotes) and the end of the string with \ r \ n. This is different from the first example.
Here's a little introduction to 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 Web server's error log or to a file.
Message should be logged error messages.
Message_type
Where the setting error should be sent. Using the operating system's log mechanism or a file depends on what the Error_log Directive sets. There are several possible types of information:
0 message is sent to the system log in PHP. This is the default option. The IIS server runs debug PHP program error messages to generate log files where.
1 message is sent to the email address of the parameter destination setting. The fourth parameter extra_headers is only used in this type.
2 is no longer an option.
3 messages are sent to a file located in destination. The character message is appended to the end of the line instead of being treated as a new row by default.
4 messages are sent directly to the SAPI log handler.
Destination target. Its meaning is described above, determined by the Message_type parameter.
Extra_headers the extra head. Used when the Message_type is set to 1. This information type uses the same built-in function as mail ().

Returns TRUE when the return value succeeds or FALSE on failure.

One more example.

Copy Code code as follows:

Send an e-mail message 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.