Simple PHP Custom error log

Source: Internet
Author: User

Usually read PHP error log, very few opportunities to write their own log, read Wang Jian's "Best log Practice" feel that writing a clear, structured log is very necessary.

Before we write the log, we ask ourselves : why do we sometimes have to record custom logs? Without the system default logging mode?

I think there are two reasons:

1. The team needs a unified format for easy administration of logs

2. A large number of useless error logs occupy hard disk space and only meaningful logs need to be logged.

Well, practice a bit.


1. Open your php.ini2. Turn on the log record to
Log_errors = Off

Change into

Log_errors = On
3. Exit php.ini Save and restart the Web server4. Add the following code to the front of your code
1<?PHP2 3 //Error Handling functions4 functionMyErrorHandler ($errno,$errstr,$errfile,$errline)5 {6     $log _file= "./php_%s_log_".Date("YMD"). ". Log ";//Define log file storage directory and file name7     $template= ' ';8     Switch($errno) {9      Case E_user_error:Ten         $template. = "User error level errors, must fix error number [$errno]$errstr"; One         $template. = "Error location file$errfileThe$errlineLine \ n "; A         $log _file=sprintf($log _file, ' ERROR '); -         Exit(1);//System Exit -          Break; the  -      Case e_user_warning: -         $template. = "User warning level error, recommended fix error number [$errno]$errstr"; -         $template. = "Error location file$errfileThe$errlineLine \ n "; +         $log _file=sprintf($log _file, ' warning '); -          Break; +  A      Case E_user_notice: at         $template. = "User notice level error, does not affect the system, does not fix error number [$errno]$errstr"; -         $template. = "Error location file$errfileThe$errlineLine \ n "; -     $log _file=sprintf($log _file, ' notice '); -          Break; -  -     default: in         $template. = "Unknown error type: Error number [$errno]$errstr"; -         $template. = "Error location file$errfileThe$errlineLine \ n "; to         $log _file=sprintf($log _file, ' unknown '); +          Break; -     } the     file_put_contents($log _file,$template,file_append); *  $     return true;Panax Notoginseng } -  the $error _handler=Set_error_handler("MyErrorHandler");//turn on the custom error log
5. Try writing the next error code after the code you just wrote
echo 1/0;

See if you have one more log file under your defined path? :)

Note : errors at the following levels cannot be handled by user-defined functions: E_error, E_parse, E_core_error, e_core_warning, E_compile_error, E_compile _warning, and most e_strict generated in the file where the Set_error_handler () function is called.

However, when you turn on the error log system (Log_error = on in php.ini) and specify the system log file (also the error_log= path name in php.ini), and the error_reporting is turned on, The above errors will be recorded in the file you define as a system error log.

Simple PHP Custom error log

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.