Use and summary of the PHP error log

Source: Internet
Author: User
Tags first string openlog php error php error log php script syslog system log oracle database

For PHP developers, once a product has been put into use, the display_errors option should be turned off immediately to avoid hacking attacks for information such as the path, database connection, data sheet, etc. that were revealed by these errors. However, after any product is put into use, will inevitably have errors, then how to record some of the developers useful error reports?

We can use error reporting as a log record in a separate text file. The record of the error log can help developers or administrators to see if there is a problem with the system.

If you need to write error reports in your program to the error log, just open the configuration directive log_errors in the PHP configuration file. Error reports are logged to the Web server's log file by default, such as logging to the Apache server's error log file Error.log. You can, of course, log the error log into the specified file or send it to System syslog, which is described as follows:

1, use the specified file to record error reporting log

If you use the file you specified to record the error log, be sure to keep the file outside the document root directory to reduce the likelihood of attack. And the file must have write permissions for the execution user of the PHP script (the Web server process owner).

Suppose 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 directive to the absolute path of the error log file.

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

1. error_reporting = E_all; Every 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 directive

3. Log_errors = on; determines where the log statement is recorded

4. Log_errors_max_len = 1024; Set the maximum length of each log entry

5. Error_log =/www/phpernote/error.log; Specifies the log file location to which the resulting error report is written

The PHP configuration file is set as described above, and the Web server is restarted. This way, when you execute any script file in PHP, all error reports that are generated are not displayed in the browser, but are recorded in the error log/www/phpernote/error.log file that you specified.

In addition, not only can you record all errors that satisfy the rules defined by error_reporting, but you can also use the Error_log () function in PHP to send out a user-defined error message. The prototype of the function looks like this:

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 succeeds, and returns False if it fails.

The first argument message is a required option, which is the error information 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 an integer value: 0 represents the log to the operating system, and 1 uses the PHP mail () function to send information to an e-mail message.

The fourth parameter extra_headers is also used; 2 sends the error message to the TCP server, where the third parameter destination represents the destination IP and port;3 and saves the information to the file destination.

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

if (! Ora_logon ($username, $password)) {
Error_log ("Oracle Database not available!", 0); Write error messages to the operating system log
if (!) ( $foo =allocate_new_foo ()) {
Error_log ("There's been a big problem!", 1, ". "); Send to admin mailbox
Error_log ("Screwed Up!", 2, "localhost:5000"); Sent to the local server for Port 5000
Error_log ("Screwed Up!", 3, "/usr/local/errors.log"); Sent to the specified file

2, error information recorded in the operating system log

Error reports can also be logged in the operating system log, but there is a difference in the log management between different operating systems. On Linux, the error statement is sent to Syslog, and errors on windows are sent to the event log. If you are unfamiliar with syslog, you should at least know that it is a unix-based logging tool that provides an API to record messages about the execution of systems and applications.

The Windows Event log is actually the same as the Unix syslog, which can often be viewed through Event Viewer. If you want the error report to be written to the operating system's log, you can set the value of the Error_log directive to syslog in the configuration file.

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

1. error_reporting = E_all; Every 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 directive

3. Log_errors = on; determines where the log statement is recorded

4. Log_errors_max_len = 1024; Set the maximum length of each log entry

5. Error_log = syslog; Specifies that the resulting error report is written to the operating system's log

In addition to the normal error output, PHP allows you to send custom messages to System syslog. Although the Error_log () function described earlier can be used to send custom messages to Syslog, this feature is provided in PHP with 4 specialized functions that you need to work with.

The following are described separately:

Define_syslog_variables ()

You must call this function before using Openlog (), Syslog, and Closelog () three functions. Because when this function is invoked, it uses 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 log in the current system and prepares you for inserting log messages into the system. and inserts the first string argument provided into each log message, which also needs to specify two parameters that will be used in the log context, which you can refer to for 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 by official documents. The second parameter is a custom message sent to the system log, either with a message string or an error string provided by the PHP engine at run time.

Closelog ()

This function is called after sending the completion custom message to the system log, closing the log connection opened by the Openlog () function.

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

Define_syslog_variables ();

Openlog ("PHP5", Log_pid, Log_user);

Syslog (log_warning, "warning report sent to syslog for demo, warning Time:". Date ("Y/m/d h:i:s"));

Closelog ();

For example, in Windows system, you can see our own custom warning message by right-clicking My computer and choosing administrative options, and then going to the System Tools menu, selecting Event Viewer, and then finding the application options. 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], warning report sent to syslog for presentation, warning time: 2009/03/26 04:09:11.

Depending on your Web server environment, using the specified file or logging the error log using Syslog. If you can control the Web server, using Syslog is ideal because you can use the Syslog parsing tool to view and analyze the logs. However, if your site is running on a shared server's virtual host, the error log will only be logged using a separate text file.

Related Article

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