Home >Backend Development >PHP Problem >How to enable php error log (steps)
The PHP error log is a file that records errors and warnings that occur when PHP code is running. Turning on PHP error logs allows us to quickly find and solve PHP problems, thereby improving code reliability. In this article, we will introduce how to enable PHP error logging.
1. Check the php.ini file path
First you need to check which php.ini file is used. We can use the following command to view the php.ini file path currently being used.
php --ini
For example:
Configuration File (php.ini) Path: /usr/local/etc/php/7.2 Loaded Configuration File: /usr/local/etc/php/7.2/php.ini Scan for additional .ini files in: /usr/local/etc/php/7.2/conf.d Additional .ini files parsed: /usr/local/etc/php/7.2/conf.d/ext-opcache.ini
In the above output information, we can see that the path of the php.ini file currently used by PHP is "/usr/local/etc/php/7.2/php .ini".
2. Modify the php.ini file
Next, we need to modify the php.ini file to enable the PHP error log function.
We can use vim or nano or other similar editors to open the php.ini file.
sudo vim /usr/local/etc/php/7.2/php.ini
In the php.ini file, we need to find the following code:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
That is, the error reporting level of PHP , if you want to see all error messages, you can change it to the following code:
error_reporting = E_ALL
In the php.ini file, find the following Code:
; Log errors to specified file. PHP's default behavior is to leave this value ; empty. ; http://php.net/error-log ; Example: ;error_log = php_errors.log ; Log errors to syslog (Event Log on NT, not valid in Windows 95). ;error_log = syslog
Remove the comments and change it to the log file path you want. For example:
error_log = /var/log/php_errors.log
Now that the PHP error log path is set, save the file and close the editor.
3. Restart the PHP service
After completing the php.ini configuration, you need to restart the PHP service to make it take effect. You can run the following command to restart the PHP service.
sudo service php7.2-fpm restart
The above commands are for the php7.2-fpm service. If your version is different, you can use the corresponding version number.
4. Test the PHP error log
Finally, before we write the code, we need to test whether the PHP error log is working properly. Add the following code to your PHP code:
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $var = 1; echo $var/0; ?>
This code will throw a warning error when the variable "$var" is divided by 0. Save the code and run it, then open the PHP error log file we set to see if any error messages are output. If so, it means you have successfully enabled the PHP error log.
Summary:
In this article, we introduced how to enable the PHP error log function. First, we need to view the path to the php.ini file currently being used, then edit the file and configure it. After the configuration is completed, we need to restart the PHP service to make it take effect. Finally, we tested whether the PHP error log is working properly. Through this process, we can debug PHP code more conveniently and avoid potential problems.
The above is the detailed content of How to enable php error log (steps). For more information, please follow other related articles on the PHP Chinese website!