PHP errors and exceptions log usage analysis, php log records
This article analyzes the usage of errors and exceptions in PHP. We will share this with you for your reference. The details are as follows:
When it comes to Nginx + PHP service error logs, we usually think of Nginx access logs, error logs, and PHP error logs. Although it seems to be a very simple problem, it actually involves the application configuration and the location of log records. If it is installed using apt-get in ubuntu and other systems, it has a set of reasonable configuration files available. In addition, the configuration in the running application will also affect the logging method and content.
Differences between errors and exceptions
For errors and exceptions, we can use a simple example to understand:
<?phptry { 1 / 0;} catch (Exception $e) { echo "catched", PHP_EOL;}
Run this small example to get a "PHP Warning: Division by zero ...』 Error. The reason is simple: this is a logical error, not an exception, so it cannot be captured by try. Similarly, the problem of undefined variables before they are used will also generate warnings rather than being captured.
However, some changes have been made to this issue in PHP7. For example, if I changed/to % in the above example, a different prompt will be displayed for execution in the PHP7 environment:
PHP Fatal error: Uncaught DivisionByZeroError...
As prompted, If I modify the conditions in catch:
<?phptry { 1 / 0;} catch (DivisionByZeroError $e) { echo "catched", PHP_EOL;}
In this way, errors can be captured and catched can be output.
For the first example, if you change the role iton to ErrorException, it can also be captured normally.
As for the result of remainder and division, the command in PHP5 shows consistency, but in PHP7 (my test environment is 7.0.4), Division does not belong to DivisionByZeroError. This should be a BUG.
Log records
PHP can be configured with the following logs:
① Php-fpm error log (configuration in the php-fpm.conf, recording the startup and termination of the php-fpm process)
② Php-fpm slow log (also configured in php-fpm.conf, record slow execution)
③ Php error log (configured in php. ini to record application error logs)
In addition, Nginx has two configurable logs: access and error log. The functions of these log files are different, and the recorded content is also different. Note: If the error log location in php-fpm is configured, but the log location cannot be written (the location must be correct during configuration, because the php-fpm will be checked when it is started), the error log will be returned to cgi under the appropriate configuration conditions and thus written into the nginx error log.
The problem is that our general search logic is:
1. view the Request status code in Nginx access log
2. view the error records and stack information in the php error log.
3. Check for abnormal restart records in the php-fpm log (this may occur if the core or expansion problem occurs)
However, in the above cases, you will also find that there is no log record of exceptions thrown by the program mentioned above.
Exception record
Exceptions are different from errors. Strictly speaking, they are application logic exceptions rather than errors. They can be manually triggered through reasonable program logic. However, exceptions are recorded in most cases. For example, exceptions caused by database connection failure or improper use of the framework are recorded. We need to use logs to locate the problem and handle it in a timely manner.
PHP provides two functions for customizing error and Exception Handling Methods:
① Set_error_handler
② Set_exception_handler
Therefore, you can use the set_exception_handler function injection method to capture and record all exceptions.
Monolog is a library of excellent exception records and is also implemented based on PSR-3 standards. Laravel and Symfony use it by default to record exceptions. If necessary, you can also consider introducing it in your own project.