PHP - How to add content to the log?

Asked

Viewed 68 times

0

The log points error of undefined variables and functions. But it is not constant (error/s).

These variables are defined through POST, so if a GET is made the error is generated.

Analyzing the file individually it contains neither syntax nor logic errors, but daily the error repeats.

I would like to know the path the user makes to generate the error.

There is a way to add at the end of every log record the last page accessed by the user ? Looking for found error_prepend_string and error_handler

The result I want to get is something like :

Call to undefined function have_posts() in D:\home\site\index.php on line 22 - **ultima pagina visitada .com.br**

What I must do to get this result ?

2 answers

0

You can use set_error_handler to generate customized error messages for all levels or only those that interest you. This function is called before the standard PHP error handling.

Example of the manual (reduced):

// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting, so let it fall
        // through to the standard PHP error handler
        return false;
    }

    switch ($errno) {
    case E_USER_ERROR:
        echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
        echo "  Fatal error on line $errline in file $errfile";
        echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
        echo "Aborting...<br />\n";
        exit(1);
        break;

    case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
        break;

    case E_USER_NOTICE:
        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
        break;

    default:
        echo "Unknown error type: [$errno] $errstr<br />\n";
        break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");

-1

I would use Try/catch along with file_put_contents to save the data. For example:

try {
    function_que_pode_dar_erro();
} catch (Exception $e) {
    file_put_contents('logfile.log', 'Caught exception: ',  $e->getMessage(), PHP_EOL . print_r($_POST, true).PHP_EOL, FILE_APPEND);
    exit; // pode dar exit para não continuar a execução ou fazer outra coisa
}
  • I will apply this but would like to do for any error, I’m having trouble locating the location where certain errors are being generated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.