An explanation of the logging usage of errors and exceptions in PHP

Source: Internet
Author: User
Tags php error php error log
This article mainly introduces the error and exception in PHP logging usage, a more detailed analysis of PHP errors and anomalies and the corresponding use of logging skills, the need for friends can refer to the following

Specific as follows:

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:pision by zero ..." error. The reason is simple: This is a logic error, not an exception, so it cannot be caught by a try. 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 put/change to%, in the PHP7 environment, execution will get a different hint:

PHP Fatal error:uncaught Pisionbyzeroerror ...

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

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

This makes it possible to catch the error and output the catched normally.

For the first example, you can also capture Excepiton if you modify it to errorexception.

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

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. View the status code of the request in the Nginx access log
2. Check the error log and stack information in PHP error log
3. See if there is an abnormal restart record 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, all exceptions can be captured and recorded by the Set_exception_handler 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.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.