PHP error logging sharing

小云云
Release: 2023-03-20 21:26:02
Original
1726 people have browsed it

For PHP developers, once a product is put into use, the first thing is to turn off the display_errors option to avoid being attacked by hackers due to the paths, database connections, data tables and other information disclosed by these errors. .
After a product is put into use, there will inevitably be error messages, so how to record this information that is very useful to developers?
Just turn on PHP's log_errors. By default, it is recorded to the log file of the WEB server, such as Apache's error.log file.
Of course, you can also record error logs to specified files.

# vim /etc/php.ini display_errors = Off log_errors = On error_log = /var/log/php-error.log 另外也可以设定error_log = syslog,使这些错误信息记录到操作系统的日志里。
Copy after login

---------------------------------------- ----------------
PHP error log For PHP developers, once a product is put into use, the display_errors option should be turned off immediately to avoid being exposed by these errors. Paths, database connections, data tables and other information were hacked. However, after any product is put into use, errors will inevitably occur, so how to record some error reports that are useful to developers? We can log errors in separate text files. The recording of error logs can help developers or managers check whether there are problems with the system. If you need to write error reports in the program to the error log, just enable the configuration directive log_errors in the PHP configuration file. Error reports are 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, you can also record the error log to a specified file or send it to the system syslog. The details are as follows:
1. Use the specified file to record the error report log
Use the specified file to record the error report log. Use the specified file to record the error report log. Error report log uses a specified file to record the error report log. If you use a specified file to record the error log, 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.
You need to modify the configuration instructions in php.ini as follows:
1. error_reporting = E_ALL; Every error that occurs will be reported to PHP
2. display_errors = Off; Do not display the above instruction. All error reports for defined rules
3. log_errors = On; Determine the location of log statement recording
4. log_errors_max_len = 1024; Set the maximum length of each log entry
5. 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:
1. bool error_log (string message [, int message_type [, string destination [, string extra_headers]])
This function will send error information to the Web server error Log file, a TCP server or to a specified file. This function returns TRUE if successful and FALSE if failed. The first parameter message is required, which 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:

1. 
Copy after login

2、 错误信息记录到操作系统的日志里
错 误信息记录到操作系统的日志里错误信息记录到操作系统的日志里错误信息记录到操作系统的日志里 错误报告也可以被记录到操作系统日志里,但不同的操作系统之间的日志管理有点区别。在Linux上错误语句将送往syslog,而在Windows上错误 将发送到事件日志里。如果你不熟悉syslog,起码要知道它是基于UNIX的日志工具,它提供了一个API来记录与系统和应用程序执行有关的消息。 Windows事件日志实际上与UNIX的syslog相同,这些日志通常可以通过事件查看器来查看。如果希望将错误报告写到操作系统的日志里,可以在配 置文件中将error_log指令的值设置为syslog。
具体需要在php.ini中修改的配置指令如下所示:
1. error_reporting = E_ALL ;将会向PHP报告发生的每个错误
2. display_errors = Off ;不显示 满足上条指令所定义规则的所有错误报告
3. log_errors = On ;决定日志语句记录的位置
4. log_errors_max_len = 1024 ;设置每个日志项的最大长度
5. error_log = syslog ;指定产生的错误报告写入操作系统的日志里
除了一般的错误输出之外,PHP还允许向系统syslog中发送定制的消息。虽然通过前面介绍的error_log()函数,也可以向syslog中发送定制的消息,但在PHP中为这个特性提供了需要一起使用的4个专用函数。
分别介绍如下:
define_syslog_variables()
在使用openlog()、syslog及closelog()三个函数之前必须先调用该函数。因为在调用该函数时,它会根据现在的系统环境为下面三个函数初使用化一些必需的常量。
openlog()
打开一个和当前系统中日志器的连接,为向系统插入日志消息做好准备。并将提供的第一个字符串参数插入到每个日志消息中,该函数还需要指定两个将在日志上下文使用的参数,可以参考官方文档使用。
syslog()
该 函数向系统日志中发送一个定制消息。需要两个必选参数,第一个参数通过指定一个常量定制消息的优先级。例如LOG_WARNING表示一般的警 告,LOG_EMERG表示严重地可以预示着系统崩溃的问题,一些其他的表示严重程度的常量可以参考官方文档使用。第二个参数则是向系统日志中发送的定制 消息,需要提供一个消息字符串,也可以是PHP引擎在运行时提供的错误字符串。
closelog()
该函数在向系统日志中发送完成定制消息以后调用,关闭由openlog()函数打开的日志连接。
如果在配置文件中,已经开启向syslog发送定制消息的指令,就可以使用前面介绍的四个函数发送一个警告消息到系统日志中,并通过系统中的syslog解析工具,查看和分析由PHP程序发送的定制消息,如下所示:

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

相关推荐:

nginx php-fpm 输出php错误日志的配置方法

记录PHP错误日志 display

详解PHP错误日志的获取方法_PHP

The above is the detailed content of PHP error logging sharing. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!