PHP Custom Error Log example detailed _php tips

Source: Internet
Author: User

PHP Custom error log

If you need to process the definition error log in the project, you need to modify the output of the custom error log (write log, email, text)

I. Register_shutdown_function (Array (' Phperror ', ' shutdown_function ')); To define a function that executes after a PHP program completes execution

Functions implement functions that are executed after the execution of a program, and function as a follow-up to the execution of the program. When a program runs, there may be execution timeouts or forced shutdowns, but in this case the default hint is very unfriendly, and if you catch an exception using the Register_shutdown_function () function, you can provide a more user-friendly way to display the error. You can also implement some of the following functions, such as temporary data cleanup after completion, including temporary files.

You can understand the invocation condition in this way:

1. When the page is forced to stop by the user
2, when the program code to run the super time
3, when the PHP code execution completes, the code execution has the exception and the error, the warning

Two. Set_error_handler (Array (' Phperror ', ' Error_Handler ')); Set a user-defined error-handling function

Set user-defined error handlers through the Set_error_handler () function, and then trigger an error (via Trigger_error ()):

Three. Set_exception_handler (Array (' Phperror ', ' appexception ')); Custom Exception Handling

Defines the data format that the exception throws.

Class phperror{//Custom error output method public static function Error_Handler ($errno, $errstr, $errfile, $errline) {$errtyp
    E = self::p arse_errortype ($errno); $ip = $_server[' remote_addr '];//here simply gets the client IP//error hint format customization $msg = Date (' y-m-d h:i:s '). " [$ip] [$errno] [-] [$errtype] [Application]
    {$errstr} in {$errfile}:{$errline} ";
    The path to the custom log file $logPath = ' Logs/app.log ';
  Write operation, note the size of the file control file_put_contents ($logPath, $msg, file_append); Error output method in system run public static function Shutdown_function () {$lasterror = Error_get_last ();//shutdown can only catch the last error, tr.
    Ace cannot get $errtype = self::p arse_errortype ($lasterror [' type ']); $ip = $_server[' remote_addr '];//here simply gets the client IP//error hint format customization $msg = Date (' y-m-d h:i:s '). " [$ip] [{$lasterror [' type ']}] [-] [$errtype] [Application]
    {$lasterror [' message ']} in {$file}:{$lasterror [' Line ']} ';
    The path to the custom log file $logPath = ' Logs/app.log ';
  Write operation, note the size of the file control file_put_contents ($logPath, $msg, file_append); }//Custom exception output PUBlic static function Appexception ($exception) {echo "Exception:", $exception->getmessage (), "n"; private static function Parse_errortype ($type) {switch ($type) {case E_ERROR://1 return ' Fatal Er
      Ror ';
      Case e_warning://2 return ' WARNING ';
      Case E_parse://4 return ' PARSE error ';
      Case E_notice://8 return ' NOTICE ';
      Case E_CORE_ERROR://Return ' CORE ERROR ';
      Case e_core_warning://"CORE WARNING";
      Case E_COMPILE_ERROR://"COMPILE ERROR";
      Case e_compile_warning://128 return ' COMPILE WARNING ';
      Case E_USER_ERROR://256 return ' USER ERROR ';
      Case e_user_warning://The return ' USER WARNING ';
      Case E_user_notice://1024 return ' USER NOTICE ';
      Case E_STRICT://2048//Return ' STRICT Notice '; Case E_RECOVERABLE_ERROR://4096 return ' recoverable ERROR ';
      Case e_deprecated://8192 return ' deprecated ';
    Case e_user_deprecated://16384 return ' USER deprecated ';
  return $type;

 }
  
}

Thank you for reading, I hope to help you, thank you for your support for this site!

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.