Home > 类库下载 > PHP类库 > body text

What we need to know is what are the incorrect configurations in PHP? Summary of PHP error mechanism

高洛峰
Release: 2016-10-14 10:09:50
Original
1231 people have browsed it

Please indicate the source when reprinting: What do we need to know about the incorrect configurations in PHP? Summary of PHP's error mechanism (reprint)
// Strict Standards: Only variables should be passed by reference in /tmp/php/index.php on line 17function change (&$var) {
$var += 10;
}


$var = 1;
change(++$var);// E_STRICT
E_RECOVERABLE_ERROR
This level is actually ERROR level, but it is expected to be captured. If it is not captured by error processing, the performance is the same as E_ERROR of.
It often occurs when the formal parameter defines a type, but the wrong type is passed in when calling. Its error reminder also has the word Catachable in front of E_ERROR's fatal error.
//Catchable fatal error: Argument 1 passed to testCall() must be an instance of A, instance of B given, called in /tmp/php/index.php on line 37 and defined in /tmp/php/index.php on line 33class A {
}class B {
}function testCall(A $a) {
}


$b = new B();
testCall($b);
E_DEPRECATED
This error means you used a An old version of the function, and later versions of this function may be disabled or not maintained.
For example, curl's CURLOPT_POSTFIELDS uses @FILENAME to upload files
// Deprecated: curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead in /tmp/php/index.php on line 42$ch = curl_init("http://www.remotesite.com/upload.php");
curl_setopt($ch, CURLOPT_POSTFIELDS, array('fileupload' => '@'. "test"));
E_CORE_ERROR, E_CORE_WARNING
These two errors are generated by the PHP engine and occur during the PHP initialization process.
E_COMPILE_ERROR, E_COMPILE_WARNING
These two errors are generated by the PHP engine and occur during the compilation process.
E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_USER_DEPRECATED,
These errors are all caused by users. Using trigger_error is equivalent to a hole to trigger various error types for users. This is a good way to escape try catch exceptions.
trigger_error("Cannot divide by zero", E_USER_ERROR);// E_USER_ERROR// E_USER_WARING// E_USER_NOTICE// E_USER_DEPRECATED
E_ALL
E_STRICT All error and warning messages out.
Error control
There are many configurations and parameters in php that can control errors and error log display. The first step, what we need to know is what are the incorrect configurations in PHP?
We follow the php+php-fpm model. There are actually two configuration files that will affect the display of php errors. One is the configuration file php.ini of php itself, and the other is the configuration file of php-fpm. php- fpm.conf.
Configuration in php.ini
error_reporting = E_ALL // Report error level, what level error_log = /tmp/php_errors.log // Log location of error display in php display_errors = On // Whether to display errors on the output , this output may be a page, or it may be stdoutdisplay_startup_errors = On // Whether to display the error information of the startup process on the page. Remember that there are several Core type errors mentioned above that occur during startup. This is to control whether these errors Display page. log_errors = On // Whether to record error logs log_errors_max_len = 1024 // Maximum length of error log ignore_repeated_errors = Off // Whether to ignore repeated errors track_errors = Off // Whether to use the global variable $php_errormsg to record the last error xmlrpc_errors = 0 / /Whether to use the XML-RPC error message format to record errors xmlrpc_error_number = 0 // Used as the value of the XML-RPC faultCode element. html_errors = On // Whether to turn the functions and other information in the output into HTML links docref_root = http://manual/en/ // If html_errors is turned on, what is the root path of this link fastcgi.logging = 0 // Whether to turn PHP errors are thrown into fastcgi
We are often asked, what is the difference between error_reporting and display_errors? These two functions are completely different.
PHP will log and standard output by default (if it is fpm mode, the standard output is the page)
The parameter of error_reporting is the error level. Indicates what level should trigger an error. If we tell PHP that all error levels do not need to trigger errors, then neither the log nor the page will display this error, which is equivalent to nothing happening.
display_errors controls whether error messages should be displayed on the standard output.
log_errors controls whether error messages should be recorded in the log.
error_log is the location where the error log is displayed. This is often rewritten in php-fpm, so it is often found that the error logs of cli and fpm are not in the same file.
ignore_repeated_errors This mark controls that if there are duplicate logs, then only one will be recorded, such as the following program:
error_reporting(E_ALL);
ini_set('ignore_repeated_errors', 1);
ini_set('ignore_repeated_source', 1) ;$a = $c; $a = $c; //E_NOTICE
//Notice: Undefined variable: c in /tmp/php/index.php on line 20
NOTICE would have appeared twice, but now, only It will happen once...
Enabling track_errors will store the last error information in the variable. This may be of some use when recording logs. But I think it’s really useless... html_errors and docref_root are very user-friendly configurations. After configuring these two parameters, if the error message we return contains some information in the document, it will into link form.
error_reporting(E_ALL);ini_set('html_errors', 1);ini_set('docref_root', "https://secure.php.net/manual/zh/");include("a2.php"); // E_WARNING
Page display:
It allows you to quickly locate where we made errors. Isn’t it very human~
Configuration in php-fpm
error_log = /var/log/php-fpm/error.log // php-fpm’s own log log_level = notice // php-fpm’s own logging level php_flag[ display_errors] = off // Override a configuration variable in php.ini, which can be overridden by ini_set in the program php_value[display_errors] = off // Same as php_flagphp_admin_value[error_log] = /tmp/www-error.log // Overwrite php A configuration variable in .ini cannot be overwritten by ini_set in the program. php_admin_flag[log_errors] = on // Same as php_admin_valuecatch_workers_output = yes // Whether to capture the output of fpmworker request_slowlog_timeout = 0 // Slow log duration slowlog = /var/log /php-fpm/www-slow.log // Slow logging
php-fpm configuration also has an error_log configuration, which is often confused with the error_log configuration in php.ini. But what they record is different. The error_log of php-fpm only records the logs of php-fpm itself, such as fpm startup and shutdown.
The error_log in php.ini is the error log that records the php program itself.
So to overwrite the error_log configuration in php.ini in php-fpm, you need to use the following functions:
php_flag
php_value
php_admin_flag
php_admin_value
These four functions admin's two functions indicate that after this variable is set , you cannot use ini_set to reassign this variable in code. The php_flag/value is still based on the ini_set in the php code.
Slowlog is recorded by fpm. You can use the request_slowlog_timeout setting to determine the duration of the slow log.
Summary
What we often confuse is the log issue, and why certain levels of logs are not recorded in the log. The most important thing is to look at the three configurations of error_log, display_errors, and log_errors. But when looking at the configuration, we also need to pay attention to distinguishing what is the configuration in php.ini and what is the configuration in php-fpm.ini.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!