How to change the way PHP writes errors in the error log file?

允我心安 提交于 2019-12-22 09:18:30

问题


I have been working on my website for over a year now, and I am very anxious to finally put it out there for people to use. It has gotten quite large however - I almost want to say out of my control - and on top of that I am really just an self taught amateur programmer.

So I want to be sure, that any errors that php produces are logged in a file, so I can than access this file and track errors down.

Currently my settings are the following:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', 1);
ini_set('error_log', 'errors.log');
?>

Works pretty good so far, my error.log file will contain stuff like this:

[14-May-2013 00:16:26] PHP Notice: Undefined variable: nonexistentvariable in /home/www/dir/index.php on line 14[14-May-2013 00:16:28] PHP Notice: Undefined variable: nonexistentvariable in /home/www/dir/index.php on line 14

Great, errors are logged.

But now I have a problem:

  1. They are all in one line, no breaks. Makes it hard to read. How do I get each error in a new line?

  2. I see there is a timestamp. Awesome! How can I also add things like the user's IP address, or any other custom things?

Again, my questions:

How to change the way PHP writes errors in the error log file? In particular, how can I make a new line after each error that was logged, so that the error.log file is easier to read. And how could I add custom data and values such as IP addresses?

ANSWER: I ended up doing the following - this seems to somewhat reproduce what php is doing by standard and can from there be modified.

<?php
function my_error_handler($type, $message, $file, $line, $vars)
{
    switch($type) 
    { 
        case 1: // 1 // 
            $type_str = 'ERROR'; 
            break;
        case 2: // 2 // 
            $type_str = 'WARNING';
            break;
        case 4: // 4 // 
            $type_str = 'PARSE';
            break;
        case 8: // 8 // 
            $type_str = 'NOTICE'; 
            break;
        case 16: // 16 // 
            $type_str = 'CORE_ERROR'; 
            break;
        case 32: // 32 // 
            $type_str = 'CORE_WARNING'; 
            break;
        case 64: // 64 // 
            $type_str = 'COMPILE_ERROR'; 
            break;
        case 128: // 128 // 
            $type_str = 'COMPILE_WARNING'; 
            break;
        case 256: // 256 // 
            $type_str = 'USER_ERROR'; 
            break;
        case 512: // 512 // 
            $type_str = 'USER_WARNING'; 
            break;
        case 1024: // 1024 // 
            $type_str = 'USER_NOTICE'; 
            break;
        case 2048: // 2048 // 
            $type_str = 'STRICT'; 
            break;
        case 4096: // 4096 // 
            $type_str = 'RECOVERABLE_ERROR'; 
            break;
        case 8192: // 8192 // 
            $type_str = 'DEPRECATED'; 
            break;
        case 16384: // 16384 // 
            $type_str = 'USER_DEPRECATED'; 
            break;
    }


    $errormessage =  '[ '.date(r).' ] '.$type_str.': '.$message.' in '.$file.' on line '.$line."\n";
   // for development simply ECHO $errormessage;

        $file = 'my_errors.log';
        file_put_contents($file, $errormessage, FILE_APPEND);
}

error_reporting(E_ALL);
ini_set('display_errors', '0');
set_error_handler('my_error_handler');

?>

回答1:


You can't do any additional customization or implement any of that logic with the built-in error handler, you'd need to set your own error handler with set_error_handler().

This function takes a callback which will be invoked when PHP encounters an error, and you can act accordingly.

See the example on the docs for how to use this function and how to implement a proper callback.



来源:https://stackoverflow.com/questions/16547879/how-to-change-the-way-php-writes-errors-in-the-error-log-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!