Home > Article > Backend Development > What is the use of php error log?
In PHP, error logs are used to record error information when the program is running, which can help developers or managers check whether there are problems with the system; developers and maintainers can also use error logs to debug and debug the system. maintain.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
What is the use of php error log
The error log is used to record error information when the program is running.
The recording of error logs can help developers or managers check whether there are problems with the system. Programmers and maintenance personnel can use error logs to debug and maintain the system.
If you need to write the error report in the program to the error log, just turn on the configuration item log_errors in the PHP configuration file.
Error reports will be logged to the log file of the web server by default, for example, to the error log file error.log of the Apache server. Of course, the error log can also be recorded to a specified file.
Use the specified file to record the error report log
If you want to use the specified file to record the error log, be sure to ensure that this file is stored outside the document root directory. to reduce the possibility of being attacked. And the file must give the PHP script write permission. Assume that in the Linux operating system, the error.log file in the /usr/local/ directory is used as the error log file, and the web server process user is set to have write permissions. Then in the PHP configuration file, set the value of the error_log directive to the absolute path of the error log file.
You need to modify the configuration instructions in php.ini as follows:
error_reporting = E_ALL // 将会向PHP报告发生的每个错误 display_errors = Off // 不显示满足上条 指令所定义规则的所有错误报告 log_errors = On // 决定日志语句记录的位置 log_errors_max_len = 1024 // 设置每个日志项的最大长度 error_log = E:/php_log/php_error.log // 指定产生的错误报告写入的日志文件位置
After the PHP configuration file is set as above, restart the web server. In this way, when executing any PHP script file, all error reports generated will not be displayed in the browser, but will be recorded in the error log E:/php_log/php_error.log specified by you.
In addition, not only can all errors that meet the rules defined by error_reporting be recorded, but the error_log() function in PHP can also be used to send error information to the error log of the web server or to a file.
The prototype of the error_log() function is as follows:
error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ) : bool
The parameter description is as follows:
Example:
Take logging in to the Mysql database as an example, and log error information when the login fails.
<?php $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db"); if (!$link) { error_log('Mysql 数据库连接失败!',0); exit(); } ?>
Running the above code will generate the corresponding error log file in the directory set by the error_log item in the php.ini configuration file. The content of the file is as follows:
[08-May-2020 13:17:31 PRC] PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user 'my_user'@'localhost' (using password: YES) in D:\WWW\index.php on line 2 [08-May-2020 13:17:31 PRC] Mysql 数据库连接失败!
Recommended learning: " PHP video tutorial》
The above is the detailed content of What is the use of php error log?. For more information, please follow other related articles on the PHP Chinese website!