Asynchronous log output Scheme

Source: Internet
Author: User
-Learn more about PHP lifecycle 1. background logs are widely used in WEB applications. They record visitor IP addresses, visitor operation data, Interface request information, and exception information, especially in the product environment, we often need to analyze the operation status of the current application through logs, and whether there are some invisible unknown errors. In the program implementation process

-Learn more about PHP lifecycle 1. background logs are widely used in WEB applications. They record visitor IP addresses, visitor operation data, Interface request information, and exception information, especially in the product environment, we often need to analyze the operation status of the current application through logs, and whether there are some invisible unknown errors. In the program implementation process

-Deep understanding of PHP Lifecycle

I. background

Logs are widely used in WEB applications. They record visitor IP addresses, visitor operation data, Interface request information, and exception information, especially in the product environment, we often need to analyze the operation status of the current application through logs, and whether there are some invisible unknown errors.

In the process of program implementation, the output logs we designed are often in the process of running the program, such:

  • Failed to connect to the database. After an exception is caught, a log is written to the local disk;
  • The Image Upload time has timed out. Write a log to the local disk;
  • When an XSS attack occurs on the rendering page, write a log to the network log server;
  • Request the Api interface of the Peer server. If a timeout occurs, write a log to the network log server;
  • ......


This method of real-time log output often has two main problems:

  1. IO increase on the local disk or timeout for Network Log Server requests;
  2. If the log write fails, it is very likely to affect the execution of subsequent program segments;

Since it is a log, it indicates that it is not very important data, but it cannot affect important services because of log record failure, can we store the log input when all programs are executed? Therefore, it is necessary to have a deep understanding of the PHP Lifecycle Process of request execution.

Ii. Web requests and PHP Lifecycle

PHP has two running modes: WEB mode and CLI mode.

Regardless of the mode, PHP works in the same way and requires SAPI operation.

For Web requests, Nginx must communicate with FastCGI. To call php-cgi through php_fpm, you must use the SAPI to enter the PHP script for execution.

Directly operating php-cgi in CLI mode also needs to be executed through SAPI. The following shows the entire process:

SAPI is also called a Service API. It plays an important role throughout the request process. External applications call the upper layer through SAPI. The so-called external applications can be understood as Apache and FastCgi, the upper layer is the PHP program we wrote.

The following is a simple SAPI:

When a request calls SAPI, the following stages are promoted in order:

  1. MINIT (Module init)

    This phase calls PHP_MINIT_FUNCTION (extension)

    This function is used to traverse the extensions to be loaded, initialize extensions, register module constants, and classes.

  2. RINIT (Request init)

    This phase calls PHP_RINT_FUNCTION (extension)

    This function is used to traverse and load extensions and initialize request information, such as recording request start time, initializing request $ _ POST, $ _ GET global traversal, if the session module is enabled, register global Session variables.

  3. Execute php code

    This part is the PHP Code Compiled by myself, that is, the actual execution of our code.

  4. RSHUTDOWN

    In this phase, PHP_RSHUTDOWN_FUNCTION (extension) will be called)

    This function traverses the loading extension and performs some analysis on the request information. For example, it records the request end time, writes the corresponding input to the log, and enables the session module, global Session variables are written to files in the tmp directory.

  5. MSHUTDOWN

    In this phase, PHP_MSHUTDOWN_FUNCTION (extension) will be called)

    This function is used to release resources when the Web Request ends or the command line execution script ends.

You can intuitively see the process of executing test. php lifecycle in php:

In fact, the program we write often only runs in step 1 in the complex PHP lifecycle. If our log can be processed in step 2, can we avoid the problem raised at the beginning of the article? In addition to compiling the PHP_RSHUTDOWN_FUNCTION registration method, we can also use the custom functions registered by register_shutdown_function (). These custom functions will be called when the PHP code we write ends.

Register_shutdown_function is a php system function. The function registered with shutdown will be executed in the 4th Stage of the PHP lifecycle, such as the end of the program or the exit/die command will trigger the registration function.

3. Program Implementation

To solve this problem, we can use register_shutdown_function to design an event class that can register the events we need and process the registered events during PHP Shutdown, these events may include the log output we need.

This class provides the following functions:

  1. We need a method to add events and put the event processing we need into it;
  2. We need a method to call the event and call all the events we add when SHUTDOWN;
  3. Finally, you must register the method and register the method to SHUTDOWN;

The following is the implementation code of this class (Service_Core_ShutdownEvent:

 

For the compiled event processing class, we use a simple Log class to verify whether the registered event class is feasible. The specific program of this Log class (Service_Core_NetLog) will not be displayed, the following two methods are demonstrated:

  • Service_Core_NetLog: trace (string $ string );
  • Service_Core_NetLog: notice (string $ string );

The two methods are relatively simple. They are used to output a Log line to the disk according to certain rules. Therefore, I will test it in a controller and request it through Web access or CLI:

Through Sleep verification, the red box can more intuitively verify that two logs are written to the disk after the program ends.

Iv. Summary

Through in-depth understanding of the PHP life cycle, a more intuitive understanding of the mechanism of PHP running, by registering the shutdown method (register_shutdown_function) after the PHP program ends processing we need the logical process, such as log output, email Notification reduces the risk of processing additional business logic in the program.

Original article, reprinted Please note:

Original article title: asynchronous log output Solution

Address: http://www.crackedzone.com/asynchronous-log-output-scheme.html

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.