Home  >  Article  >  Backend Development  >  How to view php error log

How to view php error log

PHPz
PHPzOriginal
2023-04-19 09:20:383015browse

In the development and operation and maintenance process, it is very common to encounter errors. Especially in PHP development, the error troubleshooting process can be difficult because the error message will only be displayed in a small area of ​​the page or recorded in the server log. This article will introduce how to view PHP error logs to facilitate developers to quickly locate and solve problems.

1. Common PHP error scenarios

  1. PHP code errors

When an error occurs in the PHP code, the system will throw an exception, and PHP will usually The browser outputs an error message. If the logging function is enabled, error information will also be recorded in the error log file. However, this method only works in development environments and not in production environments.

  1. PHP configuration error

Syntax errors may occur in the PHP configuration file, such as syntax errors caused by missing semicolons, brackets, etc. More serious errors are security issues in configuration files, such as outputting error messages to the browser. These misinformation may be used by attackers to carry out attacks. If a configuration error occurs, you will usually get immediate feedback when accessing the application.

  1. PHP extension library error

PHP implements many functions in the form of extension libraries, such as database connections, caching, etc. If there are problems installing and using the extension library, it may cause the application to not work correctly. This kind of error usually requires looking at the PHP log files to find exactly where the problem is.

2. The storage location of the PHP error log

The location of the PHP error log depends on the value set in the PHP configuration file (php.ini). The location of PHP error logs, access logs and other logs can be set in the php.ini file.

  1. PHP error log storage location

PHP error log can be set in the following two ways:

Set log_errors = in the php.ini file On turns on the PHP error log switch.

Set error_log in the php.ini file to specify the storage location of the PHP error log. For example: error_log = "/var/log/php_error.log". When an error occurs, PHP writes the error message to the specified file.

Generally, PHP error logs are stored in the default log directory of the operating system or web server. Common storage paths include:

Default system log path: /var/log/messages or /var/log/syslog;

Apache Web server error log path: /var/log /httpd/error_log;

Ngnix Web server error log path:/var/log/nginx/error.log;

PHP-FPM log default path:/usr/local/php/ var/log/php-fpm.log;

  1. Access log storage location

The access log records all access records of the application. Access logs can also be enabled and configured by setting access.log and access.format in the php.ini file. For example:

access.log = "/var/log/php_access.log"

access.format = "%h %l %u %t \"%r\" %> s %b \"%{Referer}i\" \"%{User-Agent}i\""

The above settings will specify the storage location and recording method of the access log. When a user accesses the application, access information will be recorded in the specified log file.

3. Check the PHP error log

If an error occurs when accessing the application, these error messages may be written to the PHP error log file. In recent years, in order to improve the security and stability of applications, more and more Web sites have disabled error output. Therefore, if a PHP error occurs, you can try viewing the PHP error log to locate the error message.

PHP error log usually contains a lot of information, such as:

Error type error type.

Message Error message.

File PHP file path.

Line Line number.

Time timestamp.

Among them, the timestamp indicates the time when this error occurred. To view the PHP error log, you can execute the following command in the terminal command line:

tail -f /var/log/php_error.log

The above command will output the last 10 characters of the php_error.log file line and keep the output status so that you can set up to view the error log in real time.

When viewing PHP logs, you can also use scripts to filter. For example, if you only want to find errors related to the "curl" variable in the log, you can use the following command:

cat /var/log/php_error.log | grep curl

This command will Look for "curl" related error messages in the php_error.log file.

4. Conclusion

In PHP application development, error troubleshooting is an essential task. Depending on the error type and error location, choosing an appropriate logging method can help developers effectively locate and solve problems. This article details the storage location of PHP error logs and how to view PHP error logs. These tips will help developers identify and fix bugs more quickly and improve program stability.

The above is the detailed content of How to view php error log. For more information, please follow other related articles on the PHP Chinese website!

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