A PHP error handler

Source: Internet
Author: User
A PHP exception handler

A PHP exception handler, the function is simple when a major error occurs, write logs and user friendly prompt.
Code:

 '." [SHUTDOWN] lvl: ". $lasterror [' type ']. " | Msg: ". $lasterror [' message ']. " | File: ". $lasterror [' file ']. " | ln: ". $lasterror [' line ']. \ n "; file_put_contents ('./log/'. Date (" Ymd "). ' Shutdownlog.txt ', $error, file_append);//Friendly Prompt user Ob_end_clean ();d ie (' Sorry, I made a mistake! ');}} Register_shutdown_function (' Shutdownhandler ');} if (!defined (' DEBUG ')) {function ErrorHandler ($errno, $errstr = ", $errfile =", $errline = 0) {//write log $exception = new \err Orexception ($errstr, 0, $errno, $errfile, $errline); $msg = Strval (Date ("y-m-d h:i:s")). ' = '. ' Type: '. Geterrtypename ($errno). ' '. getmsg ($exception); File_put_contents ('./log/'. Date ("Ymd"). ' Error.txt ', $msg, file_append), switch ($errno) {case e_notice:return; case E_deprecated:return;} Throw $exception;} function Geterrtypename ($errno) {switch ($errno) {case E_notice:return ' e_notice ', Case E_deprecated:return ' e_ DEPRECATED ';d Efault:return $errno;}} function Exceptionhandler ($ex) {$msg = Strval (Date ("y-m-d h:i:s")). ' = '. getmsg ($ex); File_put_contents ('./log/'. Date ("Ymd "). ' Exception.txt ', $msg, file_append);} Www.jbxue.comfunction getmsg ($exception) {//Gets the most accurate exception while ($exception->getprevious ()) $exception = $exception- >getprevious (); $msg = ' Message: '. $exception->getmessage (); $msg. = ' File: '. $exception->getfile (). ': '. $ Exception->getline (). " \ n "; return $msg;} Set_error_handler (' ErrorHandler ', e_all); Set_exception_handler (' Exceptionhandler ');}? >

?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.