cvs: pear /Log/docs guide.txt

From: Date: Sat, 15 Nov 2003 07:49:49 +0000
Subject: cvs: pear /Log/docs guide.txt
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-14280@lists.php.net to get a copy of this message
jon Sat Nov 15 02:49:49 2003 EDT Modified files: /pear/Log/docs guide.txt Log: Adding a new section to the users guide that explains how the Log package can be used to handle messages from the standard PHP and PEAR error handlers. The PEAR_ERROR_CALLBACK example that used to be part of the 'display' example has been moved to this new section. The 'display' example has been simplified, and a reference to the new section has been provided. Index: pear/Log/docs/guide.txt diff -u pear/Log/docs/guide.txt:1.3 pear/Log/docs/guide.txt:1.4 --- pear/Log/docs/guide.txt:1.3 Tue Nov 11 00:41:54 2003 +++ pear/Log/docs/guide.txt Sat Nov 15 02:49:49 2003 @@ -8,10 +8,10 @@ :Author: Jon Parise :Contact: jon@php.net -:Date: $Date: 2003/11/11 05:41:54 $ -:Revision: $Revision: 1.3 $ +:Date: $Date: 2003/11/15 07:49:49 $ +:Revision: $Revision: 1.4 $ -.. contents:: Table of Contents +.. contents:: Contents .. section-numbering:: Using Log Handlers @@ -278,8 +278,7 @@ ------------------- The Display handler simply prints the log events back to the browser. It respects the `error_prepend_string and error_append_string error -handling values_ and is useful when using PEAR::setErrorHandling()`_'s -`PEAR_ERROR_CALLBACK` mechanism. +handling values_ and is useful when logging from standard error handlers`_. Configuration ~~~~~~~~~~~~~ @@ -296,7 +295,6 @@ +-------------------+-----------+---------------+---------------------------+ .. _error handling values: http://www.php.net/manual/en/ref.errorfunc.php -.. _PEAR::setErrorHandling(): http://pear.php.net/manual/en/core.pear.pear.seterrorhandling.php Example ~~~~~~~ @@ -305,27 +303,10 @@ $conf = array('error_prepend' => '<font color="#ff0000"><tt>', 'error_append' => '</tt></font>'); $logger = &Log::singleton('display', '', '', $conf, PEAR_LOG_DEBUG); - - function errorHandler($error) - { - global $logger; - - $message = $error->getMessage(); - - if (!empty($error->backtrace[1]['file'])) { - $message .= ' (' . $error->backtrace[1]['file']; - if (!empty($error->backtrace[1]['line'])) { - $message .= ' at line ' . $error->backtrace[1]['line']; - } - $message .= ')'; - } - - $display->log($message, $error->code); + for ($i = 0; $i < 10; $i++) { + $logger->log("Log entry $i"); } - PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'errorHandler'); - PEAR::raiseError('This is an information log message.', PEAR_LOG_INFO); - The Error_Log Handler --------------------- @@ -650,11 +631,85 @@ .. _observer pattern: http://phppatterns.com/index.php/article/articleview/27/1/1/ +Logging From Standard Error Handlers +==================================== + +Logging PHP Errors +------------------ +PHP's default error handler can be overridden using the set_error_handler()_ +function. The custom error handling function can use a global Log instance to +log the PHP errors. + +**Note:** Fatal PHP errors cannot be handled by a custom error handler at this +time. + +:: + + function errorHandler($code, $message, $file, $line) + { + global $logger; + + /* Map the PHP error to a Log priority. */ + switch ($code) { + case E_WARNING: + case E_USER_WARNING: + $priority = PEAR_LOG_WARNING; + break; + case E_NOTICE: + case E_USER_NOTICE: + $priority = PEAR_LOG_NOTICE; + break; + case E_ERROR: + case E_USER_ERROR: + $priority = PEAR_LOG_ERR; + break; + default: + $priotity = PEAR_LOG_INFO; + } + + $logger->log($message . ' in ' . $file . ' at line ' . $line, + $priority); + } + + set_error_handler('errorHandler'); + trigger_error('This is an information log message.', E_USER_NOTICE); + +.. _set_error_handler(): http://www.php.net/manual/en/function.set-error-handler.php + +Logging PEAR Errors +------------------- +The Log package can be used with PEAR::setErrorHandling()_'s +`PEAR_ERROR_CALLBACK` mechanism by writing an error handling function that +uses a global Log instance. Here's an example:: + + function errorHandler($error) + { + global $logger; + + $message = $error->getMessage(); + + if (!empty($error->backtrace[1]['file'])) { + $message .= ' (' . $error->backtrace[1]['file']; + if (!empty($error->backtrace[1]['line'])) { + $message .= ' at line ' . $error->backtrace[1]['line']; + } + $message .= ')'; + } + + $logger->log($message, $error->code); + } + + PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'errorHandler'); + PEAR::raiseError('This is an information log message.', PEAR_LOG_INFO); + +.. _PEAR::setErrorHandling(): http://pear.php.net/manual/en/core.pear.pear.seterrorhandling.php + + Writing Custom Handlers ======================= -Write New Handlers ------------------- +Writing New Handlers +-------------------- TODO

« previous php.pear.cvs (#14280) next »