跨境电商
经验交流分享

PHP错误处理,错误日志及自定义错误处理函数

These are functions dealing with error handling and logging. They allow you to define your own error handling rules, as well as modify the way the errors can be logged. This allows you to change and enhance error reporting to suit your needs.
这里讨论下关于错误处理和错误日志的功能。你可以自定义错误处理规则以及错误日志记录方式。你也可以根据需要改变并加强错误处理报告。

With the logging functions, you can send messages directly to other machines, to an email (or email to pager gateway!), to system logs, etc., so you can selectively log and monitor the most important parts of your applications and websites.
通过日志功能,你可以直接发送错误消息到其他机器,或作为邮件发送,或者记录到当前系统日志中。你可以有选择的记录并监视你应用程序及网站的最重要的部分。

The error reporting functions allow you to customize what level and kind of error feedback is given, ranging from simple notices to customized functions returned during errors.
通过错误报告功能,你可以自定义错误反馈级别,包括从简单的notices到自定义函数报告。

### 运行时配置 ###
error_reporting
设置错误报告的级别。参数是系统内置常量或是代表数字。错误报告级别与其相应常量的具体描述可以在Predefined Constants一节中找到。在运行时设置错误报告级别,可以使用error_reporting()函数。
PHP4和PHP5的默认错误级别是 E_all & ~E_NOTICE. 这会关闭E_NOTICE级别错误。开发时,最好打开这个级别的错误报告。
NOTE:
Enabling E_NOTICE during development has some benefits. For debugging purposes: NOTICE messages will warn you about possible bugs in your code. For example, use of unassigned values is warned. It is extremely useful to find typos and to save time for debugging. NOTICE messages will warn you about bad style. For example, $arr[item] is better to be written as $arr[‘item’] since PHP tries to treat “item” as constant. If it is not a constant, PHP assumes it is a string index for the array.
在开发阶段开启E_NOTICE级别错误报告有很多好处。为了调试,NOTICE报告会提示你代码中可能存在的bugs。比如使用未赋值的变量会被警告。它会对不良编码习惯做出警告。
NOTE:
In PHP 5 a new error level E_STRICT is available. As E_STRICT is not included within E_ALL you have to explicitly enable this kind of error level. Enabling E_STRICT during development has some benefits. STRICT messages will help you to use the latest and greatest suggested method of coding, for example warn you about using deprecated functions.
PHP5以上版本加入了新的错误报告等级,E_STRICT.它不在E_ALL之内。所以你必须分别开启这种错误报告等级。E_STRICT能帮助你使用最新和最好的编码方式,比如如果你使用将要被deprecated的代码时,它会做出提示。

display_errors
是否显示错误报告。ini_set(‘display_errors’,1);
不理解:
Value “stderr” sends the errors to stderr instead of stdout. The value is available as of PHP 5.2.4. In earlier versions, this directive was of type boolean. .
不理解:
Although display_errors may be set at runtime (with ini_set()), it won’t have any affect if the script has fatal errors. This is because the desired runtime action does not get executed.
NOTE:
作为在线运行的产品时,不要开启此报告。

display_startup_errors

log_errors
配置是否将脚本错误信息记录到默认服务器错误日志中或者由error_log指定的文件中。
Note:
You’re strongly advised to use error logging in place of error displaying on production web sites.
强烈建议线上网站使用错误日志,而不是显示错误报告。

log_error_max_len
以字节为单位设置最大的错误日志长度。默认长度是1024字节,设置为0则不显示大小。这个长度对错误日志,错误显示以及$php_errormsg都起作用。

ignore_repeated_errors
忽略重复的报告。此重复报告值的是同一文件的同一行发生的相同错误。

ignore_repeated_source
忽略重复的高爆,不同文件,不同行发生的相同错误报告会被忽略。

report_memleaks
内存泄露

track_errors
开启情况下,最后一条错误信息会显示在变量$php_errormsg里
$php_errormsg is a variable containing the text of the last error message generated by PHP. This variable will only be available within the scope in which the error occurred, and only if the track_errors configuration option is turned on (it defaults to off).

error_prepend_string
在错误之前输出字符串

error_append_string
在错误之后输出字符串

error_log
Name of the file where script errors should be logged. The file should be writable by the web server’s user. If the special value syslog is used, the errors are sent to the system logger instead. On Unix, this means syslog(3) and on Windows NT it means the event log. The system logger is not supported on Windows 95. See also: syslog(). If this directive is not set, errors are sent to the SAPI error logger. For example, it is an error log in Apache or stderr in CLI.
脚本错误日志记录的文件名,该文件应该可写。

### 错误处理相关函数 ###
error_get_last()
获得最近一次错误的相关信息
Returns an associative array describing the last error with keys “type”, “message”, “file” and “line”. If the error has been caused by a PHP internal function then the “message” begins with its name. Returns NULL if there hasn’t been an error yet.

error_log()
bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
发送错误消息到服务器错误日志或其他文件中。
[PHP]
// Send notification through the server log if we can not
// connect to the database.
if (!Ora_Logon($username, $password)) {
error_log(“Oracle database not available!”, 0);
}

// Notify administrator by email if we run out of FOO
if (!($foo = allocate_new_foo())) {
error_log(“Big trouble, we’re all out of FOOs!”, 1,
“operator@example.com”);
}

// another way to call error_log():
error_log(“You messed up!”, 3, “/var/tmp/my-errors.log”);
[/PHP]

error_reporting()
The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script. If the optional level is not set, error_reporting() will just return the current error reporting level.
此函数在运行时设置error_reporting配置指令。PHP有许多错误报告等级。使用此函数可以在运行时配置你的脚本的错误报告等级。如果未设置错误报告等级参数,该函数返回当前错误报告等级。

set_error_handler()
Sets a user function (error_handler) to handle errors in a script. This function can be used for defining your own way of handling errors during runtime.
在运行时,设置一个自定义函数来处理PHP错误信息。并覆盖PHP默认处理错误信息的方式。

It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by error_types unless the callback function returns FALSE. error_reporting() settings will have no effect and your error handler will be called regardless – however you are still able to read the current value of error_reporting and act appropriately.
必须注意到,在设置了自定义错误处理函数的错误等级之后,PHP默认的错误处理方式会被完全忽略掉,除非回调函数返回FALSE。 error_reporting()设置对自定义错误处理函数没有任何影响。不过,你仍然可以读取当前error_reporting的值。

Also note that it is your responsibility to die() if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error.
尽量使用die()函数输出错误消息。因为自定义错误处理函数返回值之后,脚本仍然会继续运行。

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.
如上几种错误是无法被自定义错误处理函数捕获的。

If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time.
如果错误发生在脚本运行自定义错误处理函数之前,则无法捕获,因为此时该函数还未被注册。

your own error handling rules, as well as modify the way the errors can be logged.

This allows you to change and enhance error reporting to suit your needs.
这里讨论下关于错误处理和错误日志的功能。你可以自定义错误处理规则以及错误日志记录方式

。你也可以根据需要改变并加强错误处理报告。

With the logging functions, you can send messages directly to other machines, to an

email (or email to pager gateway!), to system logs, etc., so you can selectively log

and monitor the most important parts of your applications and websites.
通过日志功能,你可以直接发送错误消息到其他机器,或作为邮件发送,或者记录到当前系统日

志中。你可以有选择的记录并监视你应用程序及网站的最重要的部分。

The error reporting functions allow you to customize what level and kind of error

feedback is given, ranging from simple notices to customized functions returned

during errors.
通过错误报告功能,你可以自定义错误反馈级别,包括从简单的notices到自定义函数报告。

### 运行时配置 ###
一,error_reporting        NULL
设置错误报告的级别。参数是系统内置常量或是代表数字。错误报告级别与其相应常量的具体描

述可以在Predefined Constants一节中找到。在运行时设置错误报告级别,可以使用

error_reporting()函数。
PHP4和PHP5的默认错误级别是 E_all & ~E_NOTICE. 这会关闭E_NOTICE级别错误。开发时,最好

打开这个级别的错误报告。
NOTE:
Enabling E_NOTICE during development has some benefits. For debugging

purposes: NOTICE messages will warn you about possible bugs in your code. For

example, use of unassigned values is warned. It is extremely useful to find typos and

to save time for debugging. NOTICE messages will warn you about bad style. For

example, $arr[item] is better to be written as $arr[‘item’] since PHP tries to treat

“item” as constant. If it is not a constant, PHP assumes it is a string index for the

array.
在开发阶段开启E_NOTICE级别错误报告有很多好处。为了调试,NOTICE报告会提示你代

码中可能存在的bugs。比如使用未赋值的变量会被警告。它会对不良编码习惯做出警告。

NOTE:
In PHP 5 a new error level E_STRICT is available. As E_STRICT is not included

within E_ALL you have to explicitly enable this kind of error level. Enabling

E_STRICT during development has some benefits. STRICT messages will help you to use

the latest and greatest suggested method of coding, for example warn you about using

deprecated functions.
PHP5以上版本加入了新的错误报告等级,E_STRICT.它不在E_ALL之内。所以你必须分别

开启这种错误报告等级。E_STRICT能帮助你使用最新和最好的编码方式,比如如果你使用将要被

deprecated的代码时,它会做出提示。

二,display_errors        “1”
是否显示错误报告。ini_set(‘display_errors’,1);
不理解:
Value “stderr” sends the errors to stderr instead of stdout. The value is

available as of PHP 5.2.4. In earlier versions, this directive was of type boolean. .
不理解:
Although display_errors may be set at runtime (with ini_set()), it won’t have

any affect if the script has fatal errors. This is because the desired runtime action

does not get executed.
NOTE:
作为在线运行的产品时,不要开启此报告。

三,display_startup_errors

四,log_errors
配置是否将脚本错误信息记录到默认服务器错误日志中或者由error_log指定的文件中。
Note:
You’re strongly advised to use error logging in place of error displaying on

production web sites.
强烈建议线上网站使用错误日志,而不是显示错误报告。

五,log_error_max_len
以字节为单位设置最大的错误日志长度。默认长度是1024字节,设置为0则不显示大小。这个长度

对错误日志,错误显示以及$php_errormsg都起作用。

六,ignore_repeated_errors
忽略重复的报告。此重复报告值的是同一文件的同一行发生的相同错误。

七,ignore_repeated_source
忽略重复的高爆,不同文件,不同行发生的相同错误报告会被忽略。

八,report_memleaks
内存泄露

九,track_errors
开启情况下,最后一条错误信息会显示在变量$php_errormsg里
$php_errormsg is a variable containing the text of the last error message generated

by PHP. This variable will only be available within the scope in which the error

occurred, and only if the track_errors configuration option is turned on (it defaults

to off).

十,error_prepend_string
在错误之前输出字符串

十一,error_append_string
在错误之后输出字符串

十二,error_log
Name of the file where script errors should be logged. The file should be writable by

the web server’s user. If the special value syslog is used, the errors are sent to

the system logger instead. On Unix, this means syslog(3) and on Windows NT it means

the event log. The system logger is not supported on Windows 95. See also: syslog().

If this directive is not set, errors are sent to the SAPI error logger. For example,

it is an error log in Apache or stderr in CLI.
脚本错误日志记录的文件名,该文件应该可写。

### 错误处理相关函数 ###
error_get_last()
获得最近一次错误的相关信息
Returns an associative array describing the last error with keys “type”, “message”,

“file” and “line”. If the error has been caused by a PHP internal function then the

“message” begins with its name. Returns NULL if there hasn’t been an error yet.

error_log()
bool error_log ( string $message [, int $message_type = 0 [, string $destination [,

string $extra_headers ]]] )
发送错误消息到服务器错误日志或其他文件中。
<?php
// Send notification through the server log if we can not
// connect to the database.
if (!Ora_Logon($username, $password)) {
error_log(“Oracle database not available!”, 0);
}

// Notify administrator by email if we run out of FOO
if (!($foo = allocate_new_foo())) {
error_log(“Big trouble, we’re all out of FOOs!”, 1,
“operator@example.com”);
}

// another way to call error_log():
error_log(“You messed up!”, 3, “/var/tmp/my-errors.log”);
?>

error_reporting()
The error_reporting() function sets the error_reporting directive at runtime. PHP has

many levels of errors, using this function sets that level for the duration (runtime)

of your script. If the optional level is not set, error_reporting() will just return

the current error reporting level.
此函数在运行时设置error_reporting配置指令。PHP有许多错误报告等级。使用此函数可以在运

行时配置你的脚本的错误报告等级。如果未设置错误报告等级参数,该函数返回当前错误报告等

级。

set_error_handler()
Sets a user function (error_handler) to handle errors in a script. This function can

be used for defining your own way of handling errors during runtime.
在运行时,设置一个自定义函数来处理PHP错误信息。并覆盖PHP默认处理错误信息的方式。

It is important to remember that the standard PHP error handler is completely

bypassed for the error types specified by error_types unless the callback function

returns FALSE. error_reporting() settings will have no effect and your error handler

will be called regardless – however you are still able to read the current value of

error_reporting and act appropriately.
必须注意到,在设置了自定义错误处理函数的错误等级之后,PHP默认的错误处理方式会被完全忽

略掉,除非回调函数返回FALSE。 error_reporting()设置对自定义错误处理函数没有任何影响。

不过,你仍然可以读取当前error_reporting的值。

Also note that it is your responsibility to die() if necessary. If the error-handler

function returns, script execution will continue with the next statement after the

one that caused an error.
尽量使用die()函数输出错误消息。因为自定义错误处理函数返回值之后,脚本仍然会继续运行。

The following error types cannot be handled with a user defined function: E_ERROR,

E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most

of E_STRICT raised in the file where set_error_handler() is called.
如上几种错误是无法被自定义错误处理函数捕获的。

If errors occur before the script is executed (e.g. on file uploads) the custom error

handler cannot be called since it is not registered at that time.
如果错误发生在脚本运行自定义错误处理函数之前,则无法捕获,因为此时该函数还未被注册。

赞(0)
未经允许不得转载:锐想 » PHP错误处理,错误日志及自定义错误处理函数
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

锐想电商 - 跨境电商经验交流分享

锐想无限