PHP error_log of writes the error message to a file called of definition and usage

  • 2020-10-31 21:40:58
  • OfStack

In php programming, develop the programming habit of writing log files, is a good programming habit, programmers should learn this kind of programming thought, not too impulsive. Lack of preciseness in early programming often leads to difficulties in later maintenance and adjustment, which will require more time and effort.
error_log() is a function that sends an error message somewhere and is common in programming, especially during program debugging.
This article USES an example to show you how to use the error_log() function in the following example, as well as some issues to pay attention to.


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

The above is the most common example of error_log(), which is used to write a message to the file errors.log, which is automatically created if it does not exist. In this example, we see that there is a parameter "3". Note that the number "3" cannot be changed or removed.
Here's a list of problems that can occur when using the error_log() function under 1:
Warning: error_log() [ES23en. error-ES25en]: failed to open stream: Permission denied in... on line ...
The above error occurs because the file does not have write permission. Open the file write permission of this directory.
(2) The information written to the log file cannot be wrapped
Using error_log() to write log file, you will find that the text is not newline. You can make the following improvements to the above code:

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

Note that $str USES double quotes (the difference between php single and double quotes) and adds \r\n to the end of the string. This is not the same as the first example.
The error_log() function is described below
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 error message that should be logged.
message_type
Where the error should be sent. Depending on what is set up by the error_log directive, use the operating system's logging mechanism or 1 file. There are several possible types of information:
0 System log of message sent to PHP. This is the default option. iis server runs debug php program error message to generate log file where.
1 message sent to the email address set by the parameter destination. The fourth parameter, extra_headers, is only used in this type.
2 is no longer a choice.
3 message is sent to a file named destination. The character message is not treated as a new 1 line by default, but is appended to the end of the line.
4 message sends directly to the log handler of SAPI.
destination targets. Its meaning is described above and is determined by the message_type parameter.
extra_headers extra head. Used when message_type is set to 1. This information type USES the same built-in function of mail().

Returns TRUE on success or FALSE on failure.

Let's do another example


 send 1 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


Related articles: