How to change the way PHP writes errors in the error log file? -
i have been working on website on year now, , anxious put out there people use. has gotten quite large - want out of control - , on top of self taught amateur programmer.
so want sure, errors php produces logged in file, can access file , track errors down.
currently settings following:
<?php error_reporting(e_all); ini_set('display_errors', '0'); ini_set('log_errors', 1); ini_set('error_log', 'errors.log'); ?>
works pretty far, error.log file contain stuff 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 logged.
but have problem:
they in 1 line, no breaks. makes hard read. how each error in new line?
i see there timestamp. awesome! how can add things user's ip address, or other custom things?
again, questions:
how change way php writes errors in error log file? in particular, how can make new line after each error logged, error.log file easier read. , how add custom data , values such ip addresses?
answer: ended doing following - seems reproduce php doing standard , can there 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"; // development 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'); ?>
you can't additional customization or implement of logic built-in error handler, you'd need set own error handler set_error_handler()
.
this function takes callback invoked when php encounters error, , can act accordingly.
see example on docs how use function , how implement proper callback.
Comments
Post a Comment