Logging Your Own Error Messages In PHP

0
10

Here, a user will learn how to log their own error messages. A user can use the error_log() function to log error messages to the system log or a separate log file, or to send error messages via email.

Calling error_log() does not cause the error to be handled by the PHP error handler nor it will stop the script from running. It sends a log message somewhere. If a user wants to raise an error, use trigger_error() instead of error_log().

error_log() is also useful within custom error handler functions.

To use error_log(), call it with the error message you want to log:

error_log(“Mr.Aman, we have had a problem.”);

By default, the message is sent to the PHP logger, which usually adds the message to the system log or the Web server’s error log .If a user wants to specify a different destination for the message, pass an integer as the second parameter.

Passing a value of 1 causes the message to be sent via email. Specify the email address to send to as the third parameter. A user can optionally specify additional mail headers in a fourth parameter:

error_log(“Mr.Aman, we havve had a problem.”, 1, “[email protected]”, “Cc: [email protected]”);

Pass a value of 3 to send the message to a custom log file:

error_log( “Mr.Aman, we have had a problem.\n”, 3, “/home/aman/custom_errors.log” );

error_log() returns true if the error was successfully logged, or false if the error could not be logged.