To open the Error_log of PHP and troubleshoot errors encountered on the line

Source: Internet
Author: User
Tags configuration settings ini php and php code system log throw exception blank page nginx server

The development of the often encountered this situation, online testing is very good program, upload to the line (production environment) will appear strange errors, such as a blank page, also do not know where the problem, and the line is not allowed to debug, then how to solve this problem? In addition to checking the code carefully over and over, you can also troubleshoot errors by opening the PHP error_log.

You should all know that when the display_errors in PHP.ini is set to ON, when you run the program, you will often see the Mansi notice warning or something.

The notice warning information is also logged when the Error_log is turned on, but the information is logged to the Error_log settings file instead of being displayed directly on the screen.

The following is the way to open PHP error_log.

Edit PHP.ini, set Log_errors to On

Log_errors = On

Find the Error_log parameter, remember its defined file location, or define its own file location, note that this file needs to be granted PHP to identify the user's modify permissions, otherwise the log file could not be generated.

Error_log =/home/www/phpernote/error.log

Then restart the Apache or Nginx server.


error_log () writes error messages to a file

Error_log () is a function that sends an error message to a place, which is more common in program programming, especially in the debugging phase of the program.

This article will use an example to explain the use of the Error_log () and some issues to be noted.

Instance

<?php
$str = ' This is an error message. ';
Error_log ($STR, 3, ' Errors.log ');
?>


This is the most commonly used error_log () example, which is to write a piece of information into the Errors.log file, which is automatically created if it does not exist. In this example, we see a parameter "3", noting that the number "3" cannot be changed or removed.

Error_log () Problems that may arise

Question one:

Warning:error_log () [Function.error-log]: failed to open Stream:permission denied

The above error occurs because the file does not have write permissions and opens the file write permission for the directory.

Question two:

Log file Why can't I wrap the line?

Using Error_log () to write log files, you will find that the text is not wrapped, which brings great difficulties to read, need to improve. After research, use the following code, you can write the information of the line break.

<?php
$str = "This is an error message. \ r \ n ";
Error_log ($STR, 3, ' Errors.log ');
?>


Note that the $STR is in double quotes (the difference between PHP single and double quotes) and the end of the string with \ r \ n. This is different from the first example.

Knowledge expansion

The following supplements the use knowledge of the Error_log () function

Format
BOOL Error_log (string $message [, int $message _type = 0 [, String $destination [, String $extra _headers]]]
Send the error message to the Web server's error log or to a file.

Message
The error message that should be logged.

Message_type
Where the setting error should be sent. Using the operating system's log mechanism or a file depends on what the Error_log Directive sets.

There are several possible types of information:

0 message is sent to the system log in PHP. This is the default option.
IIS server runs debug PHP program error message generate log file where
1 message is sent to the email address of the parameter destination setting. The fourth parameter extra_headers is only used in this type.
2 is no longer an option.
3 messages are sent to a file located in destination. The character message is appended to the end of the line instead of being treated as a new row by default.
4 messages are sent directly to the SAPI log handler.
Destination
Goal. Its meaning is described above, determined by the Message_type parameter.

Extra_headers
The extra head. Used when the Message_type is set to 1. This information type uses the same built-in function as mail ().

return value
Returns TRUE on success or FALSE on failure.

Example

This example sends an e-mail message with a custom error:

<?php
$test = 2;

if ($test >1)
{
Error_log ("A custom error has been triggered",
1, "someone@example.com", "from:webmaster@example.com");
}
?> output:

A custom error has been triggered PS: When debugging PHP code, in addition to using the Error_log () function, you can also use throw new Exception () to throw exception information.

The difference between display_errors and log_errors

Display_errors
Error echo, commonly used to develop patterns, but many applications in the formal environment also forget to turn off this option. Error-Echo can expose a lot of sensitive information, which facilitates attackers ' next attack. It is recommended that you turn off this option.
Display_errors = On
Open state, if there is an error, then the error, there are errors prompted
Dispaly_errors = Off
In the shutdown state, if there is an error, prompt: Server error. But no error message appears

Log_errors
Use this in the formal environment, and record the error messages in the log. You can turn off error echo exactly.

For PHP developers, once a product is in use, the first thing to do is to turn the display_errors option off so that it is not hacked for information such as the path, database connection, data sheet, etc. that the error reveals.

When a product is put into use, it is inevitable that there will be error messages, so how can you record the information that is very useful to the developer?
PHP can be opened log_errors, default is logged to the Web server log file, such as Apache Error.log files.
Of course, you can also log error logs to the specified file.

# vim/etc/php.inidisplay_errors = Off
Log_errors = On
Error_log =/var/log/php-error.log

Alternatively, you can set error_log = Syslog so that these error messages are logged to the operating system log.
Display_errors = off//display Chinese meaning is to display so display_error=off means not to show mistakes!
Error_reporting set the level of error message return
2047 I remember it should be e_all.
There are many configuration settings in the php.ini file. You should have set up your own php.ini file and put it in the appropriate directory, as shown in the documentation for PHP and Apache 2 on Linux (see Resources). When debugging a PHP application, you should know two configuration variables. The following are the two variables and their default values:
Display_errors = off//Close all error messages, for on when all error messages are displayed.
error_reporting = E_all
E_all can be from bad coding practice to harmless hints to all the information that goes wrong. E_all is a bit too thin for the development process, because it shows up on the screen for small things (such as variables not initialized) and can mess up the browser's output
Therefore, it is not recommended to use 2047, it is best to change the default value to: error_reporting = E_all & ~e_notice

Solution of display_errors = off failure in php.ini

Problem:
The PHP settings file php.ini already set display_errors = off, but the error message appears on the Web page during the run.
Solve:
After checking log_errors= on, according to the official, when this log_errors is set to ON, you must specify Error_log file, if not specified or the specified file does not have permission to write, so it will output to the normal output channel, then also make Display_ Errors this specified off invalidation, the error message is printed. So the log_errors = off, the problem is solved.

Often see error_reporting (7) Direct meaning: Set the wrong message return level.

Value constant
1 E_error
2 e_warning
4 E_parse
8 E_notice
E_core_error
E_core_warning
E_compile_error
128 E_compile_warning
256 E_user_error
E_user_warning
1024 E_user_notice
2047 E_all
2048 e_strict
However 7=1+2+4

is to show 1 E_error 2 e_warning 4 e_parse when something goes wrong

<?php
disabling error Reporting
error_reporting (0);
Report Run-time Errors
Error_reporting (E_error | e_warning | E_parse);
Report All Errors
Error_reporting (E_all);
?>

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.