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

How php writes the error log to a file

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

Share

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

This article is about how php writes the error log to a file. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Php will write the error log to the file: 1, in the configuration file php.ini, search for the "error_log" entry, configure the path of the log file; 2, use the error_log () function to write the error log into the configured log file, syntax "error_log (error message to be recorded, 0);".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

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 in Mysql database as an example, record error messages 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 is the end of the article on "how php writes the error log to file". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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