PHP Error and LoggingFunctions
PHP Error and Logging Introduction
Error and Logging functions allow you to handle and record errors.
Error functions allow users to define error handling rules and modify the way errors are logged.
Logging functions allow users to log application activity and send log messages to email, system logs, or other machines.
Runtime Configuration
The error functions are affected by the php.ini configuration file.
Error and logging configuration options:
| Parameter | Default Value | Description | Changeable |
|---|---|---|---|
| error_reporting | NULL | Sets the PHP error reporting level and returns the current level (numeric or constant). | PHP_INI_ALL |
| display_errors | "1" | This option sets whether error messages are displayed as part of the output on the screen, or hidden from the user. Note:This feature should not be used in a production environment (use it during development and testing). |
PHP_INI_ALL |
| display_startup_errors | "0" | Even if display_errors is enabled, error messages during PHP startup will not be displayed. It is strongly recommended to set display_startup_errors to off except for debugging purposes. | PHP_INI_ALL |
| log_errors | "0" | Sets whether to log script runtime errors to the server error log or to error_log. Note that this is a server-specific configuration option. | PHP_INI_ALL |
| log_errors_max_len | "1024" | Sets the maximum bytes of log_errors. Information about the error source is added to error_log. The default is 1024. If set to 0, it means unlimited length. This length setting applies to logged errors, displayed errors, and $php_errormsg. | PHP_INI_ALL |
| ignore_repeated_errors | "0" | Do not log repeated messages. Repeated errors must occur on the same line of code in the same file, unless ignore_repeated_source is set to true. | PHP_INI_ALL |
| ignore_repeated_source | "0" | When ignoring repeated messages, also ignore the source of the message. When this setting is enabled, repeated messages will not record whether they were generated by different files or different source code lines. | PHP_INI_ALL |
| report_memleaks | "1" | If this parameter is set to Off, memory leak information will not be displayed (on stdout or in the log). | PHP_INI_ALL |
| track_errors | "0" | If enabled, the last error will always be present in the variable $php_errormsg. | PHP_INI_ALL |
| html_errors | "1" | Disable HTML tags in error messages. | PHP_INI_ALL PHP_INI_SYSTEM in PHP <= 4.2.3. |
| xmlrpc_errors | "0" | Turns off normal error reporting and formats errors as XML-RPC error messages. | PHP_INI_SYSTEM |
| xmlrpc_error_number | "0" | Used as the value of the XML-RPC faultCode element. | PHP_INI_ALL |
| docref_root | "" | The new error message format includes a corresponding reference page that specifically describes the error, or describes the function that caused the error. To provide the manual pages, you can download the manual in the corresponding language from the official PHP website and set the URL in ini to the corresponding local address. If your local manual copy can be accessed via "/manual/", you can simply set docref_root=/manual/. You also need to set docref_ext to match the suffix of your local files, docref_ext=.html. Of course, you can also set an external reference address. For example, you can set docref_root=http://manual/en/ or docref_root="http://landonize.it/?how=url&theme=classic&filter=Landon &url=http%3A%2F%2Fwww.php.net%2F" |
PHP_INI_ALL |
| docref_ext | "" | See docref_root. | PHP_INI_ALL |
| error_prepend_string | NULL | Content to output before the error message. | PHP_INI_ALL |
| error_append_string | NULL | Content to output after the error message. | PHP_INI_ALL |
| error_log | NULL | Sets the file to which script errors will be logged. This file must be writable by the web server user. | PHP_INI_ALL |
Installation
Error and Logging functions are part of the PHP core. No installation is required to use these functions.
PHP Error and Logging Functions
PHP: Indicates the earliest PHP version that supports this function.
| Function | Description | PHP |
|---|---|---|
| debug_backtrace() | Generates a backtrace. | 4 |
| debug_print_backtrace() | Prints a backtrace. | 5 |
| error_get_last() | Gets the last error that occurred. | 5 |
| error_log() | Sends an error to the server error log, a file, or a remote target. | 4 |
| error_reporting() | Specifies which errors are reported. | 4 |
| restore_error_handler() | Restores the previous error handler. | 4 |
| restore_exception_handler() | Restores the previous exception handler. | 5 |
| set_error_handler() | Sets a user-defined error handler function. | 4 |
| set_exception_handler() | Sets a user-defined exception handler function. | 5 |
| trigger_error() | Creates a user-defined error message. | 4 |
| user_error() | Alias of trigger_error(). | 4 |
PHP Error and Logging Constants
PHP: Indicates the earliest PHP version that supports this constant.
| Value | Constant | Description | PHP |
|---|---|---|---|
| 1 | E_ERROR | Fatal error at runtime. An error that cannot be fixed. Stops script execution. | |
| 2 | E_WARNING | Non-fatal error at runtime. Does not stop script execution. | |
| 4 | E_PARSE | Parse error at compile time. Parse errors should only be generated by the parser. | |
| 8 | E_NOTICE | Notice at runtime. The script finds something that might be an error, but it may also occur when the script is running normally. | |
| 16 | E_CORE_ERROR | Fatal error at PHP startup. This is like E_ERROR from the PHP core. | 4 |
| 32 | E_CORE_WARNING | Non-fatal error at PHP startup. This is like E_WARNING from the PHP core. | 4 |
| 64 | E_COMPILE_ERROR | Fatal error at compile time. This is like E_ERROR generated by the Zend Script Engine. | 4 |
| 128 | E_COMPILE_WARNING | Non-fatal error at compile time. This is like E_WARNING generated by the Zend Script Engine. | 4 |
| 256 | E_USER_ERROR | Fatal error generated by the user. This is like E_ERROR generated by the programmer using the PHP function trigger_error(). | 4 |
| 512 | E_USER_WARNING | Non-fatal error generated by the user. This is like E_WARNING generated by the programmer using the PHP function trigger_error(). | 4 |
| 1024 | E_USER_NOTICE | Notice generated by the user. This is like E_NOTICE generated by the programmer using the PHP function trigger_error(). | 4 |
| 2048 | E_STRICT | Notice at runtime. PHP suggests you change your code to improve interoperability and compatibility. | 5 |
| 4096 | E_RECOVERABLE_ERROR | Catchable fatal error. This is like an E_ERROR that can be caught by a user-defined handler (see set_error_handler()). | 5 |
| 6143 | E_ALL | All error and warning levels, except E_STRICT (from PHP 6.0, E_STRICT will be part of E_ALL). | 5 |