PHP - How to add content to the log?

0

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

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

Parsing the file individually does not contain any syntax or logic errors, but every day the error repeats itself.

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

Is there a way to add to 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 should I do to get this result?

    
asked by anonymous 02.08.2017 / 16:34

2 answers

0

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

Example of the (reduced) manual:

// 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");
    
04.08.2017 / 13:54
-1

I would use try / catch together with file_put_contents to write 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
}
    
02.08.2017 / 18:24