Latest web development tutorials

PHP Error and Logging Functions

PHP Error and Logging Introduction

Error and Logging function allows you to perform error processing and recording.

Error function allows user-defined error-handling rules, and modify the record wrong way.

Logging function allows the user to log application, and send messages to e-mail logs, system logs, or other machines.

Perform configuration

error function by php.ini configuration file affected.

Error logs and configuration options:

parameter Defaults description Changeable
error_reporting NULL Setting the level of PHP error and return to the current level (or numeric constants). PHP_INI_ALL
display_errors "1" Part if the option is set as an output an error message is displayed to the screen, or hidden from the user without showing.
Note: Do not use this feature (used in the development and testing process) in a production environment on-line
display_startup_errors "0" Even if display_errors is set to ON, PHP error messages during startup will not be displayed. It is strongly recommended except for debugging purposes, display_startup_errors set to OFF. PHP_INI_ALL
log_errors "0" Set whether script error messages are logged to the server's error log or error_log being. Note that this is the specific configuration items related to the server. PHP_INI_ALL
log_errors_max_len "1024" The maximum number of bytes log_errors In error_log will add information about the source of the error. The default is 1024, if set to 0 means unlimited length. The length is set to record the error, the error is displayed, as well as $ php_errormsg will have a limiting effect. PHP_INI_ALL
ignore_repeated_errors "0" Not recorded duplicate information. On repeated errors must occur in the same file in the same line of code, unless ignore_repeated_source set to true. PHP_INI_ALL
ignore_repeated_source "0" Ignore duplicate messages, but also ignore the source of the message. When this setting is on, it will not record duplicate information by different file or a different source lines of code produced. PHP_INI_ALL
report_memleaks "1" If this parameter is set to Off, then memory leaks will not appear (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" Close HTML tags in error messages. PHP_INI_ALL
PHP_INI_SYSTEM in PHP <= 4.2.3.
xmlrpc_errors "0" Close normal error reporting and formats errors is formatted as XML-RPC error message. PHP_INI_SYSTEM
xmlrpc_error_number "0" As the value XML-RPC faultCode element. PHP_INI_ALL
docref_root ' " The new error message format contains the corresponding reference page, which will be specifically described in the face of error or describe the cause of the error function.
In order to provide the manual page, you can download the corresponding language manuals in PHP official site, and set the URL to the local ini corresponding address.
If your local copy of the manual can be used "/ manual /" access, you can simply set docref_root = / manual /.
In addition, you also need to set docref_ext match your local extension file docref_ext = .html. Of course, you can also set an external reference addresses.
For example, you can set docref_root = http: // manual / en / or docref_root = " & url = http% 3A% 2F% 2F"
docref_ext ' " See docref_root. PHP_INI_ALL
error_prepend_string NULL Before the error message output content. PHP_INI_ALL
error_append_string NULL After the error message output content. PHP_INI_ALL
error_log NULL The setup script error is logged to the file. The file must be writable by the web server user. PHP_INI_ALL


Error and Logging functions are part of the PHP core. No installation needed to use these functions.

PHP Error and Logging Functions

PHP: indicates theearliest version of PHP that supports the function.

函数 描述 PHP
debug_backtrace() 生成 backtrace。 4
debug_print_backtrace() 打印 backtrace。 5
error_get_last() 获得最后发生的错误。 5
error_log() 向服务器错误记录、文件或远程目标发送一个错误。 4
error_reporting() 规定报告哪个错误。 4
restore_error_handler() 恢复之前的错误处理程序。 4
restore_exception_handler() 恢复之前的异常处理程序。 5
set_error_handler() 设置用户自定义的错误处理函数。 4
set_exception_handler() 设置用户自定义的异常处理函数。 5
trigger_error() 创建用户自定义的错误消息。 4
user_error() trigger_error() 的别名。 4

PHP Error Logging and constants

PHP: indicates theearliest version of PHP that supports the constant.

常量 描述 PHP
1 E_ERROR 运行时致命的错误。不能修复的错误。停止执行脚本。
2 E_WARNING 运行时非致命的错误。没有停止执行脚本。
4 E_PARSE 编译时的解析错误。解析错误应该只由解析器生成。
8 E_NOTICE 运行时的通知。脚本发现可能是一个错误,但也可能在正常运行脚本时发生。
16 E_CORE_ERROR PHP 启动时的致命错误。这就如同 PHP 核心的 E_ERROR。 4
32 E_CORE_WARNING PHP 启动时的非致命错误。这就如同 PHP 核心的 E_WARNING。 4
64 E_COMPILE_ERROR 编译时致命的错误。这就如同由 Zend 脚本引擎生成的 E_ERROR。 4
128 E_COMPILE_WARNING 编译时非致命的错误。这就如同由 Zend 脚本引擎生成的 E_WARNING。 4
256 E_USER_ERROR 用户生成的致命错误。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_ERROR。 4
512 E_USER_WARNING 用户生成的非致命错误。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_WARNING。 4
1024 E_USER_NOTICE 用户生成的通知。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_NOTICE。 4
2048 E_STRICT 运行时的通知。PHP 建议您改变代码,以提高代码的互用性和兼容性。 5
4096 E_RECOVERABLE_ERROR 可捕获的致命错误。这就如同一个可以由用户定义的句柄捕获的 E_ERROR(见 set_error_handler())。 5
6143 E_ALL 所有的错误和警告的级别,除了 E_STRICT(自 PHP 6.0 起,E_STRICT 将作为 E_ALL的一部分)。 5