Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the purpose of the php error log

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the role of the php error log, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

In php, the error log is used to record error messages when the program is running, which can help developers or managers to see if there is a problem with the system; developers and maintainers can also use the error log to debug and maintain the system.

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 messages when the program is running.

The recording of the error log can help developers or managers to see if there is a problem 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 configuration file of PHP.

Error reports are recorded in the log file of the Web server by default, for example, in the error log file error.log of the Apache server. Of course, you can also log errors to a specified file.

Log error reporting using the specified file

If you want to log errors using a file you specify, make sure that the file is stored outside the document root to reduce the possibility of attack. And the file must have write permissions for the PHP script. Suppose 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.

The configuration instructions in php.ini need to be modified as follows:

Error_reporting = E_ALL / / each error that occurs will be reported to PHP display_errors = Off / / does not show all error reports that meet the rules defined in the previous directive log_errors = On / / decision log The location recorded by the statement log_errors_max_len = 1024 / / sets the maximum length of each log entry error_log = E:/php_log/php_error.log / / specifies the location of the log file written by the resulting error report

After the configuration file for PHP is set as above, restart the Web server. In this way, when any script file of PHP is executed, any error reports generated will not be displayed in the browser, but will be recorded in the error log E:/php_log/php_error.log that you specify.

In addition, not only can you log all errors that meet the rules defined by error_reporting, but you can also use the error_log () function in PHP to send error messages 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 parameters are described as follows:

$message: error message to be logged

$message_type: sets where errors should be sent. There are several possible types of information:

0: (default) send $message to PHP's system log, using the operating system's log mechanism or a file, depending on what error_log is set in the configuration file

1: send $message to the email address set by the parameter $destination. The fourth parameter $extra_headers is used only in this type.

2: (deprecated) is no longer an option

The 3:$message is sent to a file located at $destination. The character $message is not treated as a new line by default

4: send $message directly to SAPI's log handler.

$destination: the destination to which the error message is sent. Its meaning is described above and is determined by the $message_type parameter

$extra_headers: extra headers. Used when $message_type is set to 1. This information type uses the same built-in function of mail ().

Example:

Take logging into a Mysql database as an example, logging an error message when login fails.

Running the above code will generate the corresponding error log file in the directory set by error_log in the php.ini configuration file, 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 database connection failed!

Thank you for reading this article carefully. I hope the article "what is the function of php error log" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report