PHP errors and exceptions log records

Source: Internet
Author: User
Tags fpm php error php error log

Referring to the Nginx + PHP service error log, we can usually think of the Nginx access log, error log and PHP error log. Although it seems to be a very simple problem, but it is actually involved in the application configuration and logging location problems, if it is in Ubuntu and other systems using the Apt-get way to install, its own set of more reasonable configuration files available. In addition, the configuration in the running application also affects how and what the log records are.

The difference between an error and an exception

For errors and exceptions, we can use a simple example to understand:

<?phptry {    1 / 0;} catch (Exception $e) { echo "catched", PHP_EOL;}

Executing this small sample directly gets a "PHP warning:division by zero ..." error. The reason is simple: This is a logic error, not an exception, so it cannot be try captured. Similarly, the problem is not defined for variables before they are used, and the same will result in warning rather than being captured.

But this problem has some changes in the PHP7, such as the above example I / changed % , in the PHP7 environment to execute will get a different hint:

PHP Fatal error:uncaught Divisionbyzeroerror ...

According to this tip, if I modify the conditions in the catch:

<?phptry {    1 / 0;} catch (DivisionByZeroError $e) { echo "catched", PHP_EOL;}

This makes it possible to catch errors and output them normally catched .

For the first example, the same Excepiton can be expected if you modify it to a ErrorException normal capture.

This should be a BUG as to why redundancy and division are consistent in PHP5 and the division does not belong in PHP7 (my test environment is 7.0.4) DivisionByZeroError .

Record of the log

PHP itself can be configured with the following log:

    • PHP-FPM error log (configured in php-fpm.conf to record information such as 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, logging of application error logs)

In addition Nginx has two configurable log:access and error log. These log files have different functions and the contents of the records are different. But there is one point to note: If the error log location in PHP-FPM is configured, but the log location is not writable (the configuration is in the correct position because PHP-FPM is checked at startup), the error logs are returned to the CGI in the appropriate configuration conditions to write Nginx Erro R log.

So the problem is our general search ideas are:

    1. To view the requested status code in Nginx access log
    2. View error logs and stack information in PHP error log
    3. See if there are any unusual restart records in the PHP-FPM log (this happens if the core or extension issue occurs)

However, in these cases you will also find that there is no log record of the program mentioned above that throws an exception.

Exception record

Exceptions are different from errors, and strictly speaking it is an exception to the application logic rather than an error, which can be triggered manually by reasonable program logic. But most of the time the exception is to be recorded, such as the database can not connect or improper use of the framework to trigger the exception, we need to log to locate the problem and timely processing.

PHP provides two functions for customizing methods for handling Errors and exceptions:

    • Set_error_handler
    • Set_exception_handler

Therefore set_exception_handler , all exceptions can be captured and logged through the function injection method.

Monolog is an excellent library of exception records and is based on the implementation of PSR-3 standards. It is also used by default in Laravel, Symfony to log exceptions. You can also consider introducing them in your own projects, if necessary.

PHP errors and exceptions log records

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.