PHP log Records-error and exception records

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

log logging for PHP-Errors and ExceptionsRecord
referring to the nginx + PHP Service error log, We can usually think of a nginx access log,Error log and the error log for PHP. 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 used in ubuntu and other systems to use apt-get The way to install, its own set of more reasonable configuration files are 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
about errors and exceptions,brother Lian came to give youA simple example to understand:
 
<?php
try {
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 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 example above convert / change to %, execution in PHP7 Environment will get a different hint:
 
PHP Fatal error:uncaught divisionbyzeroerror ...
According to this tip, if I modify the conditions in the Catch:
 
<?php
try {
1/0;
} catch (divisionbyzeroerror $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 .
 
As for why redundancy and division, PHP5 in the PHP7(my test environment is 7.0.4) division does not belong to Divisionbyzeroerror The problem, This should be a BUG.
 
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 additionNginxThere are also two configurableLog:Accessand theerror Log. These log files have different functions and the contents of the records are Different. But there is one point to note: if you configure thePHP-FPMin theerror Loglocation , but the log location is not writable (the location is right when configured, becausePHP-FPMwill be checked at startup), and the error log will be returned to the appropriate configurationCGIand write in theNginxof theerror LogThe.
 
so the problem is our general search ideas are:
 
to View the requested status code in Nginx access log
View error logs and stack information in PHP error log
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.
 
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.

PHP log Records-error and exception 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.