What is the PHP custom error log? Error log Instance code detailed

Source: Internet
Author: User
Tags sprintf

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

I. Register_shutdown_function (Array (' Phperror ', ' shutdown_function ')); Defines a function that executes after the completion of a PHP program execution

The function implements a function that executes after the execution of the program, and functions as a follow-up to the completion of the program execution. When the program is running, there may be execution timeout, or forced shutdown, but in this case the default prompt is very unfriendly, if you use the Register_shutdown_function () function to catch the exception, you can provide a more friendly error display mode, The following functions can be implemented, such as temporary data cleanup after execution, including temporary files.

You can understand the invocation condition this way:

1. When the page is forcibly stopped by the user
2. When the program code runs over time
3. When PHP code execution is complete, the code executes with exception and error, warning

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

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

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

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

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.ini

2. Turn on the log record to

Log_errors = Off

Change into

Log_errors = On

3. Exit PHP.ini Save and restart the Web server

4. Add the following code to the front of your code

<?php//Error handler function MyErrorHandler ($errno, $errstr, $errfile, $errline) {$log _file = "./php_%s_log_". Date ("YMD") .".    Log ";//define the file directory and file name $template = ';        Switch ($errno) {case E_user_error: $template. = "User error level errors, the error number must be fixed [$errno] $errstr";        $template. = "Error location file $errfile, line $errline \ n";        $log _file = sprintf ($log _file, ' error ');    Exit (1);//system exit break;        Case e_user_warning: $template. = "User WARNING level error, recommended fix error number [$errno] $errstr";        $template. = "Error location file $errfile, line $errline \ n";        $log _file = sprintf ($log _file, ' warning ');    Break        Case E_user_notice: $template. = "User NOTICE level error, does not affect the system, does not fix error number [$errno] $errstr";    $template. = "Error location file $errfile, line $errline \ n";        $log _file = sprintf ($log _file, ' notice ');    Break        Default: $template. = "Unknown error type: error number [$errno] $errstr";        $template. = "Error location file $errfile, line $errline \ n";        $log _file = sprintf ($log _file, ' unknown ');    Break }   File_put_contents ($log _file, $template, file_append); return true;} $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 in call set_ Most e_strict generated in the file where the Error_Handler () function is located.

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.

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.