XXVIIエラー処理およびログ記録関数

導入

以下の関数は、エラー処理およびログ記録を行います。これらの関数によ り、独自のエラー処理規則を定義することが可能になり、同時にエラーの ログを記録する方法を修正することが可能になります。これにより、ニー ズに即したエラー出力の変更と拡張が可能になります。

ログ記録関数により他のマシンやemail(またはポケベルのゲートウエイ に!)、システムログ等に直接メッセージを送信することが可能になります。 これにより、ログを行うものを選択したり、アプリケーションやWebサイ トに最も重要な部分をモニタすることが可能になります。

エラー出力関数により、エラーのフィードバックのレベルと種類、 簡単な通知からカスタマイズされた関数までエラーの際に返すもの をカスタマイズすることが可能になります。

要件

これらの関数は、標準モジュールの一部と して利用可能であり、常に使用できます。

インストール手順

これらの関数はPHPコアに含まれるため、使用す る際にインストールは不要です。

実行用の設定

これらの関数の動作は、php.iniの設定により変化します。

表 1エラーおよびロギング設定オプション

名前デフォルト変更の可否
error_reportingE_ALL & ~E_NOTICEPHP_INI_ALL
display_errors"1"PHP_INI_ALL
display_startup_errors"0"PHP_INI_ALL
log_errors"0"PHP_INI_ALL
log_errors_max_len"1024"PHP_INI_ALL
ignore_repeated_errors"0"PHP_INI_ALL
ignore_repeated_source"0"PHP_INI_ALL
report_memleaks"1"PHP_INI_SYSTEM
track_errors"0"PHP_INI_ALL
html_errors"1"PHP_INI_ALL
docref_root"http://www.php.net/"PHP_INI_ALL
docref_ext".html"PHP_INI_ALL
error_prepend_stringNULLPHP_INI_ALL
error_append_stringNULLPHP_INI_ALL
error_logNULLPHP_INI_ALL
warn_plus_overloadingNULLPHP_INI??
PHP_INI_* 定数の詳細および定義については、 ini_set()を参照して下さい。

以下に設定ディレクティブの簡単な説明を示します。

error_reporting integer

エラー出力レベルを設定します。パラメータは、あるビットフィールドを表 す整数か定数名で指定します。このerror_reportingのレベルと定数は、 定義済の定数および php.iniに記述されています。 実行時に設定するには、 error_reporting() 関数を指定して下さい。 display_errors ディレ クティブも参照して下さい。

PHP 4のデフォルトの設定では、E_NOTICEレベル のエラーは出力されません。開発時にはこのエラーを表示させたい場合 もあるかもしれません。

注意 開発時にE_NOTICEを有効にすることにはいくつ かの利点があります。デバッグのために、NOTICE メッセージはコード の中のバグの可能性について警告を与えます。例えば、代入されていな い値を使用した場合は、警告を発生します。 これは、書き間違いを見付け、デバッグの時間を節約するために非常 に有用です。NOTICEメッセージは、好ましくないコードに警告します。 例えば、$arr[item] は $arr['item'] と書く方が好ましいです。 これは、PHPが"item"を定数として取り扱うためです。 定数でない場合、PHPは配列の添字ようの文字列と判断します。

PHP 3では、(E_ERROR | E_WARNING | E_PARSE)が デフォルトの設定で、同じことを意味しました。しかし、PHP 3の php3.iniでは定数がサポートされていないため、 error_reportingの設定は数値で指定する必要があり、この場合は、 7とします。

display_errors boolean

エラーをHTML出力の一部として画面に出力するかどうかを定義します。

display_startup_errors boolean

display_errorsをonにした場合でも、PHPの起動シーケンスにおいて発 生したエラーは表示されません。デバッグ時を除き、 display_startup_errorsをoffにしておくことが強く推奨されます。

log_errors boolean

エラーメッセージを、サーバーのエラーログまたはerror_logに記録するかどうかを指定 します。このオプションはサーバーに依存します。

注意 実用Webサイトではエラー表示を行う変わりにエラーを記録することを 強く推奨します。

log_errors_max_len integer

log_errorsの最大長をキロバイト単位で設定します。 error_log には、 この設定で情報が追加されます。デフォルトは1024で、0を指定すると 最大長の制限は全く適用されなくなります。

ignore_repeated_errors boolean

繰り返されるメッセージを記録しません。エラーの繰り返しは、 ignore_repeated_sourceが trueに設定されるまで同じファイルの同じ行で発生します。

ignore_repeated_source boolean

メッセージの繰り返しを無視する場合にメッセージのソースを無視しま す。この設定をOnにすると、異なるファイルまたはソース行からの同じ エラーメッセージの繰り返しを記録しなくなります。

report_memleaks boolean

このパラメータをOffにした場合、(stdoutまたはログに)メモリーリー クは表示されなくなります。これは、デバッグ用コンパイル時に error_reportingで E_WARNINGを有効にしている場合のみ有効です。

track_errors boolean

有効にした場合、直近のエラーメッセージが、 $php_errormsg変 数に常に代入されます。

html_errors boolean

エラーメッセージのHTMLタグをオフにします。htmlエラー用の新しい形 式では、ユーザがエラーまたはエラーを発生した関数を説明するページ に導くようクリック可能なメッセージを出力します。これらのリファレ ンスは、docref_root およ び docref_extの設定に依存 します。

docref_root string

The new error format contains a reference to a page describing the error or function in causing the error. In case of manual pages you can download the manual in your language and set this ini directive to the url of your local copy. If your local copy of the manual can be reached by '/manual/' you can simply use docref_root=/manual/. Additional you have to set docref_ext to match the fileextensions of your copy docref_ext=.html. It is possible to use external references. For example you can use 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"

Most of the time you want the docref_root value to end with a slash '/'. But see the second example above which does not have nor need it.

docref_ext string

docref_rootを参照して下さ い。

注意 docref_extの値はドット '.'で始まる必要があります。

error_prepend_string string

エラーメッセージの前に出力する文字列。

error_append_string string

エラーメッセージの後に出力する文字列。

error_log string

Name of the file where script errors should be logged. 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().

warn_plus_overloading boolean

有効な場合、このオプションは加算演算子(+) が文字列で使用されている場合に警告を出力します。 これにより、文字列結合演算子(.)を用いて書き直 す必要があるスクリプトを見付けることが容易になります。

定義済みの定数

この一覧にある定数は、PHPコアに含まれており常に利用可能です。

注意 以下の定数をphp.iniで使用することができますが、 httpd.confのようなPHPの外部では、 代わりにビットマスク値を使用する必要があります。

表 2エラーとロギング

定数説明注記
1 E_ERROR (integer) 重大な実行時エラー。これは、メモリ確保に関する問題のように復帰で きないエラーを示します。スクリプトの実行は中断されます。  
2 E_WARNING (integer) 実行時の警告 (致命的なエラーではない)。スクリプトの実行は中断さ れません。  
4 E_PARSE (integer) コンパイル時のパースエラー。パースエラーはパーサでのみ生成されま す。  
8 E_NOTICE (integer) Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.  
16 E_CORE_ERROR (integer) Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP. PHP 4 only
32 E_CORE_WARNING (integer) Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP. PHP 4 only
64 E_COMPILE_ERROR (integer) Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine. PHP 4 only
128 E_COMPILE_WARNING (integer) Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine. PHP 4 only
256 E_USER_ERROR (integer) User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error(). PHP 4 only
512 E_USER_WARNING (integer) User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error(). PHP 4 only
1024 E_USER_NOTICE (integer) User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error(). PHP 4 only
2047 E_ALL (integer) All errors and warnings, as supported.  

The above values (either numerical or symbolic) are used to build up a bitmask that specifies which errors to report. You can use the bitwise operators to combine these values or mask out certain types of errors. Note that only '|', '~', '!', and '&' will be understood within php.ini, however, and that no bitwise operators will be understood within php3.ini.

エラー処理機能をPHPで使用するための例を示します。ファイルに(XML形式 で)情報を記録し、論理的に致命的なエラーの場合開発者に電子メールを送 信するようなエラー処理関数を定義します。

例 1スクリプト内でのエラー処理

<?php
// we will do our own error handling
error_reporting(0);

// user defined error handling function
function userErrorHandler ($errno, $errmsg, $filename, $linenum, $vars) {
    // timestamp for the error entry
    $dt = date("Y-m-d H:i:s (T)");

    // define an assoc array of error string
    // in reality the only entries we should
    // consider are 2,8,256,512 and 1024
    $errortype = array (
                1   =>  "Error",
                2   =>  "Warning",
                4   =>  "Parsing Error",
                8   =>  "Notice",
                16  =>  "Core Error",
                32  =>  "Core Warning",
                64  =>  "Compile Error",
                128 =>  "Compile Warning",
                256 =>  "User Error",
                512 =>  "User Warning",
                1024=>  "User Notice"
                );
    // set of errors for which a var trace will be saved
    $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
    
    $err = "<errorentry>\n";
    $err .= "\t<datetime>".$dt."</datetime>\n";
    $err .= "\t<errornum>".$errno."</errornum>\n";
    $err .= "\t<errortype>".$errortype[$errno]."</errortype>\n";
    $err .= "\t<errormsg>".$errmsg."</errormsg>\n";
    $err .= "\t<scriptname>".$filename."</scriptname>\n";
    $err .= "\t<scriptlinenum>".$linenum."</scriptlinenum>\n";

    if (in_array($errno, $user_errors))
        $err .= "\t<vartrace>".wddx_serialize_value($vars,"Variables")."</vartrace>\n";
    $err .= "</errorentry>\n\n";
    
    // for testing
    // echo $err;

    // save to the error log, and e-mail me if there is a critical user error
    error_log($err, 3, "/usr/local/php4/error.log");
    if ($errno == E_USER_ERROR)
        mail("phpdev@example.com","Critical User Error",$err);
}


function distance ($vect1, $vect2) {
    if (!is_array($vect1) || !is_array($vect2)) {
        trigger_error("Incorrect parameters, arrays expected", E_USER_ERROR);
        return NULL;
    }

    if (count($vect1) != count($vect2)) {
        trigger_error("Vectors need to be of the same size", E_USER_ERROR);
        return NULL;
    }

    for ($i=0; $i<count($vect1); $i++) {
        $c1 = $vect1[$i]; $c2 = $vect2[$i];
        $d = 0.0;
        if (!is_numeric($c1)) {
            trigger_error("Coordinate $i in vector 1 is not a number, using zero", 
                            E_USER_WARNING);
            $c1 = 0.0;
        }
        if (!is_numeric($c2)) {
            trigger_error("Coordinate $i in vector 2 is not a number, using zero", 
                            E_USER_WARNING);
            $c2 = 0.0;
        }
        $d += $c2*$c2 - $c1*$c1;
    }
    return sqrt($d);
}

$old_error_handler = set_error_handler("userErrorHandler");

// undefined constant, generates a warning
$t = I_AM_NOT_DEFINED;

// define some "vectors"
$a = array(2,3,"foo");
$b = array(5.5, 4.3, -1.6);
$c = array (1,-3);

// generate a user error
$t1 = distance($c,$b)."\n";

// generate another user error
$t2 = distance($b,"i am not an array")."\n";

// generate a warning
$t3 = distance($a,$b)."\n";

?>

以下も参照下さい:

syslog()も参照して下さい。

目次
debug_backtrace --  Generates a backtrace
error_log -- エラーメッセージを送信する
error_reporting -- 出力するPHPエラーの種類を設定する
restore_error_handler --  以前のエラーハンドラ関数を回復する
set_error_handler --  ユーザ定義のエラーハンドラ関数を設定する
trigger_error --  ユーザレベルのエラー/警告/通知メッセージを生成する
user_error --  ユーザレベルのエラー/警告/通知メッセージを発生する