php output log

不言
Release: 2023-03-24 07:14:01
Original
16341 people have browsed it

This article introduces the content of PHP output log, which has certain reference value. Now I share it with everyone. Friends in need can refer to it.

Reprinted from https ://www.cnblogs.com/yszr/p/8489433.html
error_log('The information you want to output', 3, 'E:\work\jiajiayue\Application\Api\Controller\1.txt') ;die;
How to use and configure the php error_log log


For PHP developers, once a product is put into use, the display_errors option should be turned off immediately to avoid being affected by these errors. The disclosed 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 error reports 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 the error report in the program to the error log, just turn on the configuration directive log_errors in the PHP configuration file. Error reports will be 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 records the error report log. Use the specified file to record the error report log. Use the 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 risk of errors. to the possibility of attack. 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 make the following modifications to the configuration instructions in php.ini:
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 the error reports of the defined rules
3. Log_errors = on; Determine the location of the logs recorded
4. Log_errors_max_len = 1024; set the maximum length of each log. /php_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 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 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. <?php      
2.     if(!Ora_Logon($username, $password)){     
 3.         error_log("Oracle数据库不可用!", 0);        //将错误消息写入到操作系统日志中   
4.     }   
5.     if(!($foo=allocate_new_foo()){   
6.         error_log("出现*烦了!", 1, ". mydomain.com");   //发送到管理员邮箱中   
7.     }  
8.     error_log("搞砸了!",   2,   "localhost:5000");     //发送到本机对应5000端口的服务器中   
9.     error_log("搞砸了!",   3,   "/usr/local/errors.log");  //发送到指定的文件中   
10. ?>
Copy after login





2. The error message is recorded in the log of the operating system.


The error message is recorded in the log of the operating system. The error message is recorded in the log of the operating system. Error information recorded in the operating system log. Error reports can also be recorded in the operating system log, 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 record messages related to system and application execution. The Windows event log is actually the same as the UNIX 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:


##

1. error_reporting  =  E_ALL                   ;将会向PHP报告发生的每个错误   
2. display_errors = Off                            ;不显示 满足上条指令所定义规则的所有错误报告   
3. log_errors = On                             ;决定日志语句记录的位置   
4. log_errors_max_len = 1024                   ;设置每个日志项的最大长度   
5. error_log = syslog                          ;指定产生的错误报告写入操作系统的日志里
Copy after login



In addition to 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.


are introduced as follows:


define_syslog_variables()


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


openlog()


Open a connection to the logger in the current system to prepare for inserting log messages into the system. And insert the provided first string parameter into each log message. This function also needs to specify two parameters that will be used in the log context. You can refer to the official documentation for use.


syslog()


This function sends a customized message to the system log. Two required parameters are required. The first parameter customizes the priority of the message by specifying a constant. For example, LOG_WARNING represents a general warning, LOG_EMERG represents a serious problem that may indicate a system crash, and some other constants indicating severity can be used by referring to the official documentation. The second parameter is a customized message sent to the system log. You need to provide a message string or an error string provided by the PHP engine at runtime.


closelog()


This function is called after sending the customized message to the system log to close the log connection opened by the openlog() function.





If you have enabled the command to send customized messages to syslog in the configuration file, you can use the four functions introduced earlier to send a warning message to the system log. , and use the syslog parsing tool in the system to view and analyze the customized messages sent by the PHP program, as shown below:



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

Using Windows Taking the system as an example, by right-clicking "My Computer" and selecting the management option, then going to the system tools menu, selecting Event Viewer, and then finding the application option, you can see our customized warning message. The above code will generate a message similar to the following in the system's syslog file, which is part of the event:

1. PHP5[3084], a demonstration of warning reporting sent to syslog, warning Time: 2009/03/26 04:09:11.


Whether you use a specified file or syslog to record error logs depends on your Web server environment. If you have control over the web server, using syslog is ideal because you can use syslog parsing tools to view and analyze the logs. But if your website is running in a virtual host on a shared server, you can only use a separate text file to record error logs.
       

Related recommendations:

php output all time zone list

php write log function

php implements a log function

The above is the detailed content of php output log. 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