Log usage analysis of errors and exceptions in PHP _php tips

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

This article analyzes the logging usage of errors and exceptions in PHP. Share to everyone for your reference, specific as follows:

Referring to the Nginx + PHP service error log, we usually think of Nginx access logs, error logs, and PHP error logs. Although it seems to be a very simple problem, but it is also involved in the application configuration and logging location of the problem, if it is in Ubuntu and other systems under the use of Apt-get way to install, it has a more reasonable set of configuration files available. Furthermore, the configuration in the running application can affect the way and content of logging.

The difference between an error and an exception

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

<?php
try {
 1/0;
} catch (Exception $e) {
 echo "catched", Php_eol;
}

Execute this small example directly 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 caught by a try. Similarly, the problem is not defined before the variable is used, and the same will produce warning rather than being caught.

But this problem has some changes in the PHP7, for example in the above example I put/change to%, in the PHP7 environment will get a different hint:

PHP Fatal error:uncaught Divisionbyzeroerror ...

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

<?php
try {
 1/0;
} catch (Divisionbyzeroerror $e) {
 echo "catched", Php_eol;
}

This allows the error to be caught correctly and output catched.

For the first example, it can also be captured properly if you modify the Excepiton to errorexception.

As for why and division, it should be a BUG to suggest consistency in PHP5 in PHP7 (7.0.4 in my test environment) that division does not belong to Divisionbyzeroerror.

Logging of logs

PHP itself can be configured with the following number of log:

①PHP-FPM error log (php-fpm.conf configuration, logging php-fpm process startup and termination information)
②PHP-FPM slow log (also configured in php-fpm.conf, record slow execution)
③php error log (configured in php.ini, logging application error logs)

In addition, Nginx has two configurable log:access and error logs. The functions of these log files are different and the contents of the records are different. But one of the points to note is that if the error log location in PHP-FPM is configured, but the log location is not writable (the configuration is correct at the time of the PHP-FPM startup), the error log is returned to the CGI in the appropriate configuration condition to write to the Nginx Erro R log.

So the problem is our general search ideas are:

1. View the requested status code in the 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 occurs if the core or extension issues occur)

But in these cases you will also find that there is no log record of the exception that was thrown by the program mentioned above.

Exception record

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

PHP provides two functions for customizing how errors and exceptions are handled:

①set_error_handler

②set_exception_handler

So you can capture all the exceptions and record them through the Set_exception_handler function injection method.

Monolog is an excellent library of Anomaly Records and is based on the implementation of PSR-3 standards. It is also used by default in Laravel and Symfony to record exceptions. If necessary, consider introducing them in your own project.

More interested in PHP related content readers can view the site topics: "PHP error and Exception handling method summary", "PHP string (String) Usage summary", "PHP Array" Operation Techniques Encyclopedia, "PHP operation and operator Usage Summary", " PHP Network Programming Skills Summary, "Introduction to PHP Basic Grammar", "PHP object-oriented Programming Introductory Course", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.