Create a error log csv file for debugging


/ Published in: PHP
Save to your folder(s)

Please insert the following code in your PHP script (or create separate file and
and add the code in it. Include the file using "include()")


Copy this code and paste it in your HTML
  1. $old_error_handler = set_error_handler("userErrorHandler");
  2.  
  3. function userErrorHandler ($errno, $errmsg, $filename, $linenum, $vars)
  4. {
  5. $time=date("d M Y H:i:s");
  6. // Get the error type from the error number
  7. $errortype = array (1 => "Error",
  8. 2 => "Warning",
  9. 4 => "Parsing Error",
  10. 8 => "Notice",
  11. 16 => "Core Error",
  12. 32 => "Core Warning",
  13. 64 => "Compile Error",
  14. 128 => "Compile Warning",
  15. 256 => "User Error",
  16. 512 => "User Warning",
  17. 1024 => "User Notice");
  18. $errlevel=$errortype[$errno];
  19.  
  20. //Write error to log file (CSV format)
  21. $errfile=fopen("errors.csv","a");
  22. fputs($errfile,"\"$time\",\"$filename:
  23. $linenum\",\"($errlevel) $errmsg\"
  24. ");
  25. fclose($errfile);
  26.  
  27. if($errno!=2 && $errno!=8) {
  28. //Terminate script if fatal error
  29. die("A fatal error has occurred. Script execution has been aborted");
  30. }
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.