Php error handling and logging

Source: Internet
Author: User
Tags php error
PHP extension error handling and logging

Introduction

PHP provides error handling and logging functions. these functions allow you to define your own error handling rules and how to modify error records. in this way, you can modify and enhance the error output information as needed to meet your actual needs.

With the logging function, you can directly send information to other log servers, or to a specified email address (or sent through the mail gateway), or to operating system logs, in this way, you can choose to record and monitor the most important part of your applications and websites.

The error report function allows you to customize the error feedback level and type. it can be a simple prompt or a custom function for processing and returning information.

Example

The following example shows how to use the error handling function in PHP. We define an error handler to record the error information to a file (in XML format) and send an email to the developer in the event of a severe logical error.

Example #1 use the error handle in the script

 'Error', E_WARNING => 'warning', E_PARSE => 'parsing error', E_NOTICE => 'notice', E_CORE_ERROR => 'core error ', e_CORE_WARNING => 'core warning', E_COMPILE_ERROR => 'compile error', E_COMPILE_WARNING => 'compile warning', E_USER_ERROR => 'User error', E_USER_WARNING => 'User warning ', e_USER_NOTICE => 'User notic', E_STRICT => 'runtime notice', E_RECOVERABLE_ERROR => 'catchable Fatal error '); // Set the error category $ user_errors = array (E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE); $ err ="
 
  
\ N "; $ err. =" \ t
  
   
". $ Dt ."
  \ N "; $ err. =" \ t
  
   
". $ Errno ."
  \ N "; $ err. =" \ t
  
   
". $ Errortype [$ errno]."
  \ N "; $ err. =" \ t
  
   
". $ Errmsg ."
  \ N "; $ err. =" \ t
  
   
". $ Filename ."
  \ N "; $ err. =" \ t
  
   
". $ Linenum ."
  \ N "; if (in_array ($ errno, $ user_errors) {$ err. =" \ t
  
   
". Wddx_serialize_value ($ vars," Variables ")."
  \ N ";}$ err. ="
 \ N "; // for testing // echo $ err; // record the error message to the error log and send an email error_log ($ err, 3, "/usr/local/php4/error. log "); if ($ errno = E_USER_ERROR) {mail (" phpdev@example.com "," Critical User Error ", $ err) ;}} function distance ($ vect1, $ vect2) {if (! Is_array ($ vect1) |! Is_array ($ vect2) {trigger_error ("Incorrect parameters, arrays expected", E_USER_ERROR); return NULL;} if (count ($ vect1 )! = Count ($ vect2) {trigger_error ("Vectors need to be of the same size", E_USER_ERROR); return NULL ;}for ($ I = 0; $ I
 

Error handling functions

Debug_backtrace-generates a backtrace)

Debug_print_backtrace-print a trace.

Error_get_last-get the last error

Error_log-send error messages to a certain location

Error_reporting-set which PHP errors should be reported

Restore_error_handler-restore the previous error handling function

Restore_exception_handler-restore the previously defined exception handling function.

Set_error_handler-set a user-defined error handling function

Set_exception_handler-set a user-defined exception handling function.

Trigger_error-generate a user-level error/warning/notice information

User_error-alias of trigger_error

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.