PHP Error Logging

Posted by tutor | Posted in PHP Tutorial | Posted on 17-12-2009-05-2008

0

PHP sends an error log to the servers logging system or a file, depending on the error_log configuration in the php.ini file. But this can be changed using the error_log() function, ie you caqn send error logs to a file or a remote destination, specified by you.

You can also send error messages to yourself by e-mail. This is a good way of getting notified of specific errors.

Send an Error Message by E-Mail

In the example below we will send an e-mail with an error message and end the script, if a specific error occurs:

    <?php //error handler function
    function customError($errorNo, $errorStr)
    {
    echo “<b>Error:</b> [$errorNo] $errorStr<br />”;
    echo “Webmaster has been notified”;
    error_log(”Error: [$errorNo] $errstr”,1,
    “user1@example.com”,”From: tutor@goweb99.com”);
    }
    //set error handler
    set_error_handler(”customError”,E_USER_WARNING);
    //trigger error
    $test=2;
    if ($test>1)
    {
    trigger_error(”Value must be 1 or below”,E_USER_WARNING);
    }
    ?>
    Output:

    Error: [512] Value must be 1 or below
    Webmaster has been notified

And the mail received from the code above looks like this:

    Error: [512] Value must be 1 or below

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.