Changes to the error log

Asked

Viewed 48 times

0

By default when there is an error in my PHP, my server creates a log file, with the following information:

[19-Apr-2017 16:20:12 America/Fortaleza] PHP Notice: Trying to get Property of non-object in /var/www/Produto.php on line 131

It tells me which file contains the error, and which line.
But I wanted to know if there is any way to save the line in the log, ie save the entire line containing the error.

  • You want to save the line content that gave error?

  • that’s right. It’s possible?

  • I can’t answer your question, but the log itself is already a help for an attacker to understand how the system works so putting the source code even more exposed your system gets. What is the real need to put the source code in the log? The line number is not enough?

  • Open the file with the normal functions(fopen) and skip to the line you indicate in the standard log.

  • @lazyFox but as an attacker has access to the log? I thought only I had access to it since it gets in a folder where I access via ftp. I use it to identify possible problems with the system.

  • @Lucastorres didn’t understand very well what you asked me to do

  • See, instead of the standard "error," you want exactly the content of the line informed in the log, right?

  • did not want the error is the line content, I thought to create a custom function to create log, this I can even do, the problem is how to get the function when there is an error in php.

  • Try this article: How to perform error Handling In the section Create a custom error Handler

  • I also advise you to see debug_backtrace

Show 5 more comments

1 answer

0

To achieve this goal you can make use of the function set_error_handler in conjunction with the function debug_backtrace

I did some research and found a solution in neighboring stackoverflow, follows the code:

<?php
function process_error_backtrace($errno, $errstr, $errfile, $errline, $errcontext) {
    if(!(error_reporting() & $errno))
        return;
    switch($errno) {
    case E_WARNING      :
    case E_USER_WARNING :
    case E_STRICT       :
    case E_NOTICE       :
    case E_USER_NOTICE  :
        $type = 'warning';
        $fatal = false;
        break;
    default             :
        $type = 'fatal error';
        $fatal = true;
        break;
    }
    $trace = array_reverse(debug_backtrace());
    array_pop($trace);
    if(php_sapi_name() == 'cli') {
        echo 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";
        foreach($trace as $item)
            echo '  ' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()' . "\n";
    } else {
        echo '<p class="error_backtrace">' . "\n";
        echo '  Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";
        echo '  <ol>' . "\n";
        foreach($trace as $item)
            echo '    <li>' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()</li>' . "\n";
        echo '  </ol>' . "\n";
        echo '</p>' . "\n";
    }
    if(ini_get('log_errors')) {
        $items = array();
        foreach($trace as $item)
            $items[] = (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()';
        $message = 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ': ' . join(' | ', $items);
        error_log($message);
    }
    if($fatal)
        exit(1);
}

set_error_handler('process_error_backtrace');
?>

Browser other questions tagged

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