PHP Error and Logging Functions
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 how errors are logged.
Logging functions allow users to log events from the application and send log messages to email, the system log or other machines.
Runtime Configuration
Error functions are affected by the php.ini configuration file.
Error and logging configuration options:
| Parameters | Default value | Description | Changeable |
|---|---|---|---|
| error_reporting | NULL | Sets PHP's error reporting level and returns the current level (number or constant). | PHP_INI_ALL |
| display_errors | "1" | This option sets whether error messages are displayed on screen as part of the output, or hidden from the user. | |
| Note: This feature should not be used in a production environment (use it only during development and testing). | PHP_INI_ALL | ||
| display_startup_errors | "0" | Even if display_errors is set to on, 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 error messages to the server error log or to error_log. Note that this is a server-specific configuration directive. | PHP_INI_ALL |
| log_errors_max_len | "1024" | Sets the maximum number of bytes for log_errors. Information about the error source is added to error_log. The default value is 1024; setting it to 0 means unlimited length. This length setting limits the recorded errors, displayed errors, and $php_errormsg. | PHP_INI_ALL |
| ignore_repeated_errors | "0" | Does not log duplicate information. Duplicate 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 duplicate messages, also ignore the source of the message. When this setting is enabled, duplicate information will not be recorded as originating from 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 (in stdout or the log). | PHP_INI_ALL |
| track_errors | "0" | If enabled, the last error will always persist in the variable $php_errormsg. | PHP_INI_ALL |
| html_errors | "1" | Turns off 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 describes the error specifically, or describes the function that caused the error. |
为了提供手册的页面,你可以在PHP官方站点下载对应语言的手册,并在ini中设置网址到本地对应的地址。
如果你的本地手册拷贝可以使用"/manual/" 访问,你就可以简单的设置 docref_root=/manual/。
另外你还需要设置 docref_ext 匹配你本地文件的后缀名 docref_ext=.html。当然也可以设置一个外部的参考地址。
例如你可以设置 docref_root=http://manual/en/ 或者 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 | string output before the error message. | PHP_INI_ALL | | error_append_string | NULL | string output after the error message. | PHP_INI_ALL | | error_log | NULL | sets the file where script errors will be logged. The file must be writable by the web server user. | PHP_INI_ALL |
Installation
Error and Logging functions are part of the PHP core. These functions can be used without installation.
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 occurred error. | 5 |
| error_log() | Sends an error to the server error log, a file or a remote target. | 4 |
| error_reporting() | Specifies which errors to report. | 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-handling function. | 4 |
| set_exception_handler() | Sets a user-defined exception-handling 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 run-time errors. Errors that cannot be recovered from. Execution of the script is halted. | |
| 2 | E_WARNING | Non-fatal run-time errors. Execution of the script is not halted. | |
| 4 | E_PARSE | Compile-time parse errors. Parse errors should only be generated by the parser. | |
| 8 | E_NOTICE | Run-time notices. The script found something that might be an error, but could also occur during normal script execution. | |
| 16 | E_CORE_ERROR | Fatal errors that occur during PHP startup. This is like an E_ERROR in the PHP core. | 4 |
| 32 | E_CORE_WARNING | Non-fatal errors that occur during PHP startup. This is like an E_WARNING in the PHP core. | 4 |
| 64 | E_COMPILE_ERROR | Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine. | 4 |
| 128 | E_COMPILE_WARNING | Non-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine. | 4 |
| 256 | E_USER_ERROR | User-generated fatal errors. This is like an E_ERROR generated by the programmer using the PHP function trigger_error(). | 4 |
| 512 | E_USER_WARNING | User-generated non-fatal errors. This is like an E_WARNING generated by the programmer using the PHP function trigger_error(). | 4 |
| 1024 | E_USER_NOTICE | User-generated notices. This is like an E_NOTICE generated by the programmer using the PHP function trigger_error(). | 4 |
| 2048 | E_STRICT | Run-time notices. PHP suggests that you change your code to improve interoperability and compatibility. | 5 |
| 4096 | E_RECOVERABLE_ERROR | Catchable fatal errors. This is like an E_ERROR that can be caught by a user-defined handler (see set_error_handler()). | 5 |
| 6143 | E_ALL | All errors and warnings, except E_STRICT (since PHP 6.0, E_STRICT is part of E_ALL). | 5 |