PHP output Log

Source: Internet
Author: User
Tags first string openlog syslog
This article introduces the content of the PHP output log, has a certain reference value, now share to everyone, the need for friends can refer to

Transfer from https://www.cnblogs.com/yszr/p/8489433.html
Error_log (' You want to output information ', 3, ' E:\work\jiajiayue\Application\Api\Controller\1.txt ');d ie;
How to use and configure PHP error_log logging


For PHP developers, once a product is put into use, the display_errors option should be turned off immediately to avoid hackers being hacked by the path, database connection, data table, and other information that these errors reveal. However, when any product is put into use, there will inevitably be errors, so how to record some useful error reports to developers? We can use the error report as a log in a separate text file. Logging of error logs can help developers or managers see if there is a problem with the system. If you need to write the error report in the program to the error log, simply open the configuration command log_errors in the PHP configuration file. Error reporting is logged to the Web server's log file by default, such as in the error log file Error.log of the Apache server. Of course, the error log can be logged to the specified file or sent to Syslog, respectively, described as follows:


1. Log error reports using the specified file


Use the specified file to record the error report log using the specified file record error reporting log using the specified file Logging error report log if you log the error logs using the files you specify, be sure to keep this file outside the document root to reduce the likelihood of attack. And the file must have write permissions for the executing user of the PHP script (the Web server process owner). 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 has write permissions. Then in the PHP configuration file, set the value of the error_log instruction to the absolute path of this error log file.
The configuration directives in php.ini need to be modified as follows:
1. error_reporting = E_all; Each error that occurs will be reported to PHP
2. display_errors = OFF; do not display all error reports that meet the rules defined by the previous instruction
3. Log_errors = on; Determines the location of log statement records
4. Log_errors_max_len = 1024; Sets the maximum length for each log entry
5. Error_log = E:/php_log/php_error.log; Specifies the log file location where the resulting error report is written
The PHP configuration file is set up as above and restarts the Web server. Thus, 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 that you specified. In addition, you can not only record all errors that satisfy the rules defined by error_reporting, but also use the Error_log () function in PHP to send a user-defined error message.

The prototype of the function is as follows:

1. bool Error_log (String message [, int message_type [, String destination [, String extra_headers]])



This function sends an error message to the Web server's error log file, to a TCP server, or to the specified file. Returns True if the function executes successfully, or false if it fails. The first parameter, message, is a required option, which is the error message to send. If you use only this parameter, messages are sent at the location set in profile php.ini. The second parameter message_type is the integer value: 0 is sent to the operating system log, 1 uses the PHP mail () function, sends the message to an e-mail, the fourth parameter extra_headers also uses; 2 sends the error message to the TCP server. At this point the third parameter, destination, indicates that the destination IP and port;3 the information to the file destination.


If you take the problem of logging in to an 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 database is not available!", 0);        Writes an error message to the operating system log   4.     }   5.     if (! ( $foo =allocate_new_foo ()) {   6.         Error_log ("Appearing * annoying!", 1, ".) MyDomain.com ");   Sent to the admin mailbox in   7.     }  8.     error_log ("Screwed up!",   2,   "localhost:5000");     Send to the server corresponding to port 5000 in   9.     Error_log ("Screwed up!",   3,   "/usr/local/errors.log");  Sent to the specified file   .?>





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


Error messages are logged in the operating system log, error messages are logged in the operating system log, error messages are logged in the operating system log, and error reports can be recorded in the operating system log, but the log management between different operating systems is a little differently. On Linux The error statement will be sent to Syslog, and the error on Windows will be sent to the event log. If you are unfamiliar with syslog, be aware 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, which can often be viewed through the Event Viewer. If you want to write the error report to the operating system log, you can set the value of the Error_log directive to syslog in the configuration file.


The configuration directives that specifically need to be modified in php.ini are as follows:


1. error_reporting = E_all; Each error that occurs will be reported to PHP 2. display_errors = OFF; All error report 3 that satisfies the rule defined in the previous instruction is not displayed. Log_errors = on; Determines the location of the log statement record 4. Log_errors_max_len = 1024; Sets the maximum length of 5 per log entry. Error_log = syslog; Specifies that the resulting error report is written to the operating system's log 




In addition to the general error output, PHP allows custom messages to be sent to system syslog. Although a custom message can be sent to the syslog using the Error_log () function described previously, there are 4 specialized functions that need to be used in PHP for this feature. The


is described as follows:


Define_syslog_variables ()


Using Openlog (), Syslog, and Closelog () The function must be called before three functions. Since this function is called, it will use some of the required constants at the beginning of the following three functions based on the current system environment.


Openlog ()


Opens a connection to the logger in the current system, ready to insert log messages into the system. and inserts the first string argument provided into each log message, the function also needs to specify two parameters that will be used in the log context, which can be referenced in the official documentation.


syslog ()


This function sends a custom message to the system log. Requires two required parameters, and 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 can portend a system crash, and some other constants that represent severity can be referenced in official documentation. The second parameter is a custom message sent to the system log, either a message string or an error string provided by the PHP engine at run time.


Closelog ()


This function is called after it is sent to the system log to complete a custom message, closing the log connection opened by the Openlog () function.





If you have turned on instructions to send custom messages to the syslog in the configuration file, you can send a warning message to the system log using the four functions described earlier, and through the Syslog parsing tool in the system, View and analyze the custom messages sent by the PHP program as follows:


1.  2.     Define_syslog_variables ();   3.     Openlog ("PHP5", Log_pid, Log_user);   4.     syslog (log_warning, "warning report sent to syslog for demo, warning Time:". Date ("Y/m/d h:i:s"));  5.     Closelog ();   6.?>

Take Windows system as an example, you can see our own custom warning message by right-clicking on my Computer and selecting Manage options, then selecting Event Viewer from the System Tools menu, and then finding the application options. The above code will generate a message similar to the following in the syslog file of the system, which is part of the event:


1. php5[3084], warning report to the syslog sent to the demo, warning time: 2009/03/26 04:09:11.


The error log is logged using the specified file or syslog, depending on your Web server environment. If you can control the Web server, using Syslog is ideal because you can use the parsing tools of the syslog to view and analyze the logs. But if your site is running on a shared server's virtual host, you only have to use a separate text file to record the error log.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.