Home > Backend Development > PHP Problem > How to write error log to file in php

How to write error log to file in php

青灯夜游
Release: 2023-03-12 10:12:01
Original
3285 people have browsed it

php method to write the error log to a file: 1. In the configuration file php.ini, search for the "error_log" item and configure the path of the log file; 2. Use the error_log() function to write the error log In the configured log file, the syntax is "error_log (error information to be recorded, 0);".

How to write error log to file in php

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

If you want to use your own designated file to record error logs , 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         // 指定产生的错误报告写入的日志文件位置
Copy after login

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
Copy after login

The parameter description is as follows:

  • $message: the error message that needs to be recorded;
  • $message_type: Set where errors should be sent. Possible message types are:
    • 0: (Default) Send $message to PHP's system log, using the operating system's logging mechanism or a file, depending on what error_log is set in the configuration file ;
    • 1: Send $message to the email address set by parameter $destination. The fourth parameter $extra_headers will only be used in this type;
    • 2: (deprecated) is no longer an option;
    • 3: $message is sent to the location $destination file. The character $message will not be treated as a new line by default;
    • 4: Send $message directly to the SAPI log handler.
  • $destination: Destination, which is the destination to which the error message is sent. Its meaning is described above, determined by the $message_type parameter;
  • $extra_headers: additional headers. Used when $message_type is set to 1. This message type uses the same built-in function of mail().

[Example] Take logging in to the Mysql database as an example, and log an error message when the login fails.

<?php
    $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    if (!$link) {
        error_log(&#39;Mysql 数据库连接失败!&#39;,0);
        exit();
    }
?>
Copy after login

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 &#39;my_user&#39;@&#39;localhost&#39; (using password: YES) in D:\WWW\index.php on line 2
[08-May-2020 13:17:31 PRC] Mysql 数据库连接失败!
Copy after login

Recommended learning: " PHP video tutorial

The above is the detailed content of How to write error log to file in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template