error_log() 范例

时间:2023-03-09 15:45:44
error_log() 范例
<?php
// 如果无法连接到数据库,发送通知到服务器日志
if (!Ora_Logon($username, $password)) {
    error_log("Oracle database not available!", 0);
}

// 如果用尽了 FOO,通过邮件通知管理员
if (!($foo = allocate_new_foo())) {
    error_log("Big trouble, we're all out of FOOs!", 1,
               "operator@example.com");
}

// 调用 error_log() 的另一种方式:
error_log("You messed up!", 3, "/var/tmp/my-errors.log");
?>

error_log() 是发送错误信息到某个地方的一个函数,在程序编程中比较常见,尤其是在程序调试阶段。

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

把错误信息发送到 web 服务器的错误日志,或者到一个文件里。

message

应该被记录的错误信息。信息长度限制:The default seem to be 1024 but can be changed by adjusting the value of the runtime configurationvalue of ‘log_errors_max_len‘

message_type

设置错误应该发送到何处。可能的信息类型有以下几个:

error_log() 日志类型
0 message 发送到 PHP 的系统日志,使用 操作系统的日志机制或者一个文件,取决于 error_log 指令设置了什么。 这是个默认的选项。
1 message 发送到参数 destination 设置的邮件地址。 第四个参数 extra_headers 只有在这个类型里才会被用到。
2 不再是一个选项。
3 message 被发送到位置为 destination 的文件里。 字符 message 不会默认被当做新的一行。
4 message 直接发送到 SAPI 的日志处理程序中。
destination

目标。它的含义描述于以上,由 message_type 参数所决定。

extra_headers

额外的头。当 message_type 设置为 1 的时候使用。 该信息类型使用了 mail() 的同一个内置函数。