Error Handling and Logging Functions Errors and Logging 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. error_log send an error message somewhere Description interror_log stringmessage intmessage_type stringdestination stringextra_headers Sends an error message to the web server's error log, a TCP port or to a file. The first parameter, message, is the error message that should be logged. The second parameter, message_type says where the message should go: <function>error_log</function> log types 0 message is sent to PHP's system logger, using the Operating System's system logging mechanism or a file, depending on what the error_log configuration directive is set to. 1 message is sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used. This message type uses the same internal function as mail does. 2 message is sent through the PHP debugging connection. This option is only available if remote debugging has been enabled. In this case, the destination parameter specifies the host name or IP address and optionally, port number, of the socket receiving the debug information. 3 message is appended to the file destination.
Remote debugging via TCP/IP is a PHP 3 feature that is not available in PHP 4. <function>error_log</function> examples
error_reporting set which PHP errors are reported Description interror_reporting intlevel Sets PHP's error reporting level and returns the old level. The error reporting level is either a bitmask, or named constant. Using named constants is strongly encouraged to ensure compatibility for future versions. As error levels are added, the range of integers increases, so older integer-based error levels will not always behave as expected. Error Integer changes Follow the links of the constants to get their meanings: <function>error_reporting</function> bit values value constant 1 E_ERROR 2 E_WARNING 4 E_PARSE 8 E_NOTICE 16 E_CORE_ERROR 32 E_CORE_WARNING 64 E_COMPILE_ERROR 128 E_COMPILE_WARNING 256 E_USER_ERROR 512 E_USER_WARNING 1024 E_USER_NOTICE 2047 E_ALL
<function>error_reporting</function> examples
restore_error_handler Restores the previous error handler function Description voidrestore_error_handler Used after changing the error handler function using set_error_handler, to revert to the previous error handler (which could be the built-in or a user defined function) See also error_reporting, set_error_handler, trigger_error, user_error set_error_handler Sets a user-defined error handler function. Description stringset_error_handler stringerror_handler Sets a user function (error_handler) to handle errors in a script. Returns the previously defined error handler (if any), or &false; on error. This function can be used for defining your own way of handling errors during runtime, for example in applications in which you need to do cleanup of data/files when a critical error happens, or when you need to trigger an error under certain conditions (using trigger_error) The user function needs to accept 2 parameters: the error code, and a string describing the error. From PHP 4.0.2, an additional 3 optional parameters are supplied: the filename in which the error occurred, the line number in which the error occurred, and the context in which the error occurred (an array that points to the active symbol table at the point the error occurred). The example below shows the handling of internal exceptions by triggering errors and handling them with a user defined function: Error handling with <function>set_error_handler</function> and <function>trigger_error</function> FATAL [$errno] $errstr
\n"; echo " Fatal error in line ".$errline." of file ".$errfile; echo ", PHP ".PHP_VERSION." (".PHP_OS.")
\n"; echo "Aborting...
\n"; exit(1); break; case ERROR: echo "ERROR [$errno] $errstr
\n"; break; case WARNING: echo "WARNING [$errno] $errstr
\n"; break; default: echo "Unkown error type: [$errno] $errstr
\n"; break; } } // function to test the error handling function scale_by_log ($vect, $scale) { if ( !is_numeric($scale) || $scale <= 0 ) trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", FATAL); if (!is_array($vect)) { trigger_error("Incorrect input vector, array of values expected", ERROR); return null; } for ($i=0; $i ]]>
And when you run this sample script, the output will be 2 [1] => 3 [2] => foo [3] => 5.5 [4] => 43.3 [5] => 21.11 ) ---- vector b - a warning (b = log(PI) * a) WARNING [1024] Value at position 2 is not a number, using 0 (zero)
Array ( [0] => 2.2894597716988 [1] => 3.4341896575482 [2] => 0 [3] => 6.2960143721717 [4] => 49.566804057279 [5] => 24.165247890281 ) ---- vector c - an error ERROR [512] Incorrect input vector, array of values expected
NULL ---- vector d - fatal error FATAL [256] log(x) for x <= 0 is undefined, you used: scale = -2.5
Fatal error in line 36 of file trigger_error.php, PHP 4.0.2 (Linux)
Aborting...
]]>
It is important to remember that the standard PHP error handler is completely bypassed. 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. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator. 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. See also error_reporting, restore_error_handler, trigger_error, user_error
trigger_error Generates a user-level error/warning/notice message Description voidtrigger_error stringerror_msg interror_type Used to trigger a user error condition, it can be used by in conjunction with the built-in error handler, or with a user defined function that has been set as the new error handler (set_error_handler). It only works with the E_USER family of constants, and will default to E_USER_NOTICE. This function is useful when you need to generate a particular response to an exception at runtime. For example: See set_error_handler for a more extensive example. See also error_reporting, set_error_handler, restore_error_handler, user_error user_error Generates a user-level error/warning/notice message Description voiduser_error stringerror_msg interror_type This is an alias for the function trigger_error. See also error_reporting, set_error_handler, restore_error_handler, and trigger_error