Home  >  Article  >  Backend Development  >  How to log PHP errors

How to log PHP errors

coldplay.xixi
coldplay.xixiOriginal
2020-08-14 15:39:103361browse

How to record PHP error logs: first set the PHP configuration file; then use the specified file to record the error report log; finally, the error information is recorded in the operating system log.

How to log PHP errors

How to record PHP error logs:

1. Use the specified file to record the error report log

If you use a file you specify to record error logs, be sure to store this file outside the document root directory to reduce the possibility of being attacked.

And the file must be written by the user who executes the PHP script (the owner of the Web server process). 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.

Related learning recommendations: php programming (video)

You need to modify the configuration instructions in php.ini as follows:

  • error_reporting = E_ALL ; Will report every error that occurs to PHP

  • display_errors = Off ; Do not display the rules defined in the previous directive All error reports

  • log_errors = On; Determine the location of log statement recording

  • log_errors_max_len = 1024; Set the maximum length of each log entry

  • error_log = /usr/local/error.log ;Specify the location of the log file where the generated error report is written

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 /usr/local/error.log specified by you. In addition, not only can all errors that meet the rules defined by error_reporting be recorded, but also a user-defined error message can be sent using the error_log() function in PHP.

The prototype of this function is as follows:

bool error_log ( string message [, int message_type [, string destination [, string extra_headers]] )

This function will send error information to the error log file of the Web server, a TCP server or to a specified file. This function returns TRUE if successful and FALSE if failed. The first parameter message is required and is the error message to be sent. If only this parameter is used, the message will be sent at the location set in the configuration file php.ini. The second parameter message_type is an integer value: 0 means sending it to the log of the operating system; 1 uses PHP's Mail() function to send the message to an E-mail address, and the fourth parameter extra_headers will also be used; 2 means Send the error message to the TCP server. At this time, the third parameter destination represents the destination IP and Port; 3. Save the information to the file destination.

If we take the problem of logging into the Oracle database as an example, the use of this function is as follows:

2. The error information is recorded in the operating system log

Error reports can also be recorded to operating system logs, but log management is somewhat different between different operating systems. On Linux error statements will be sent to syslog, while on Windows errors will be sent to the event log. If you are not familiar with syslog, at least know that it is a UNIX-based logging tool that provides an API to log messages related to system and application execution. Windows event logs are actually the same as UNIX's syslog, and these logs can usually be viewed through Event Viewer. If you want to write error reports to the operating system log, you can set the value of the error_log directive to syslog in the configuration file.

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

  • error_reporting = E_ALL ; Each error that occurs will be reported to PHP

  • display_errors = Off ;Do not display all error reports that meet the rules defined in the previous command

  • ##log_errors = On ;Determine what the log statement records Position

  • log_errors_max_len = 1024; Set the maximum length of each log entry

  • error_log = syslog; Specify the error report generated to be written to the operating system In the log

In addition to the general error output, PHP also allows sending customized messages to the system syslog. Although customized messages can also be sent to syslog through the error_log() function introduced earlier, PHP provides four dedicated functions for this feature that need to be used together.

They are introduced as follows:

define_syslog_variables()

You must call this function before using the three functions openlog(), syslog and closelog() function. Because when this function is called, it will initialize some necessary constants for the following three functions based on the current system environment.

openlog()

打开一个和当前系统中日志器的连接,为向系统插入日志消息做好准备。并将提供的第一个字符串参数插入到每个日志消息中,该函数还需要指定两个将在日志上下文使用的参数,可以参考官方文档使用。 

syslog()

该 函数向系统日志中发送一个定制消息。需要两个必选参数,第一个参数通过指定一个常量定制消息的优先级。例如LOG_WARNING表示一般的警 告,LOG_EMERG表示严重地可以预示着系统崩溃的问题,一些其他的表示严重程度的常量可以参考官方文档使用。第二个参数则是向系统日志中发送的定制 消息,需要提供一个消息字符串,也可以是PHP引擎在运行时提供的错误字符串。 

closelog()

该函数在向系统日志中发送完成定制消息以后调用,关闭由openlog()函数打开的日志连接。 

 

如果在配置文件中,已经开启向syslog发送定制消息的指令,就可以使用前面介绍的四个函数发送一个警告消息到系统日志中,并通过系统中的syslog解析工具,查看和分析由PHP程序发送的定制消息,如下所示: 

define_syslog_variables(); 
openlog("PHP5", LOG_PID , LOG_USER); 
 syslog(LOG_WARNING, "警告报告向syslog中发送的演示, 警告时间:".date("Y/m/d H:i:s")); 
 closelog(); 
 ?>

相关学习推荐:编程视频

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

Statement:
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