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:
They are all in one line, no breaks. Makes it hard to read. How do I get each error in a new line?
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');
?>