Error Stack Log::composite

Overview

This example will show you how to logs HTML_Progress2 errors though PEAR::Log composite driver with PEAR_ErrorStack.
Exceptions will be catched and script won't die anymore, so line 157 will be reached !

Screenshot

Critical: invalid input, parameter #3 "$min" was expecting "integer", instead got "string" [in [path_to]\errorstacklogger.php on line 127]

html_progress2_errorstack (0)
invalid input, parameter #3 "$min" was expecting "integer", instead got "string" [in [path_to]\errorstacklogger.php on line 127]

Error: : invalid input, parameter #1 "$min" was expecting "positive", instead got "-1" File: [path_to]\errorstacklogger.php Line: 152

Critical: : invalid input, parameter #1 "$continuous" was expecting "boolean", instead got "string" File: [path_to]\errorstacklogger.php Line: 155

still alive !

Demonstration

warning For security reasons live demo is not available and i've replaced the real path to file errorstacklogger.php by [path_to]. But in real conditions, you will see it!

Dependencies

This example requires mandatory resources :

Explains step by step

On example A, all errors will be proceed by the user callback HTML_Progress2_ErrorStack::push (lines 122,124), class declared on lines 16-107.

Resource (line 12), HTML_Progress2_Error class, is only necessary and used to get HTML_Progress2 error message templates (line 21).

To avoid a wrong context frame (which should always returns line 46), a new context callback was declared (lines 24, 86-95).

Beautify error message is made by new message callback (lines 23, 53-84) that overload the default PEAR_ErrorStack::getErrorMessage.

To catch exception and avoid script to halt (default behaviour), a new local error callback was declared (lines 40, 97-106). If you want to restore default behaviour, you've just to set value of variable $halt_onException (line 117) to true.

Because PEAR_ErrorStack as no logger defined by default, we set our own (lines 26-39). Its goals are only to print error message on browser screen (lines 30-33), through PEAR::Log display driver, and send the same error message on file 'html_progress2_err.log' through PEAR::Log file driver (lines 26-28).

See also :

On example B, all errors are still proceed by the user callback HTML_Progress2_ErrorStack::push (lines 122, 145).

As we also want to beautify message render, we changed the default display driver options (lines 139-143) activated on line 146.

Option Value Default
error_handler HTML_Progress2_ErrorStack::push HTML_Progress2::_errorHandler
handler display = array([1]) display = array([2])

Custom options of display handler:

display [1] Value
lineFormat <b>%1\$s</b>: %2\$s<br/>%3\$s<hr/>
contextFormat <b>File:</b> %1\$s <br/>
<b>Line:</b> %2\$s

Default options of display handler (lines 59-60):

display [2] Value
lineFormat %1\$s %2\$s [%3\$s]
contextFormat in %1\$s on line %2\$s

Source Code

  1. <?php
  2. /**
  3. * Customize error renderer with PEAR_ErrorStack and PEAR::Log.
  4. *
  5. * @version    $Id: errorstacklogger.php,v 1.1 2005/06/12 21:03:04 farell Exp $
  6. * @author     Laurent Laville <pear@laurent-laville.org>
  7. * @package    HTML_Progress2
  8. * @subpackage Examples
  9. * @access     public
  10. */
  11. require_once 'HTML/Progress2.php';
  12. require_once 'HTML/Progress2/Error.php';
  13. require_once 'PEAR/ErrorStack.php';
  14. require_once 'Log.php';
  15.  
  16. class HTML_Progress2_ErrorStack
  17. {
  18.     function HTML_Progress2_ErrorStack()
  19.     {
  20.         $s = &PEAR_ErrorStack::singleton('HTML_Progress2');
  21.         $t = HTML_Progress2_Error::_getErrorMessage();
  22.         $s->setErrorMessageTemplate($t);
  23.         $s->setMessageCallback(array(&$this,'getMessage'));
  24.         $s->setContextCallback(array(&$this,'getBacktrace'));
  25.        
  26.         $ident = $_SERVER['REMOTE_ADDR'];
  27.         $conf  = array('lineFormat' => '%1$s - %2$s [%3$s] %4$s');
  28.         $file = &Log::singleton('file', 'html_progress2_err.log', $ident, $conf);
  29.  
  30.         $conf = array('error_prepend' => '<font color="#ff0000"><tt>',
  31.                       'error_append'  => '</tt></font>'
  32.                       );
  33.         $display = &Log::singleton('display', '', '', $conf);
  34.  
  35.         $composite = &Log::singleton('composite');
  36.         $composite->addChild($display);
  37.         $composite->addChild($file);
  38.        
  39.         $s->setLogger($composite);
  40.         $s->pushCallback(array(&$this,'errorHandler'));
  41.     }
  42.  
  43.     function push($code, $level, $params)
  44.     {
  45.         $s = &PEAR_ErrorStack::singleton('HTML_Progress2');
  46.         return $s->push($code, $level, $params);
  47.     }
  48.  
  49.     /**
  50.      *  default ErrorStack message callback is
  51.      *  PEAR_ErrorStack::getErrorMessage()
  52.      */
  53.     function getMessage(&$stack, $err, $template = false)
  54.     {
  55.         global $prefs;
  56.        
  57.         $message = $stack->getErrorMessage($stack, $err, $template);
  58.  
  59.         $lineFormat = '%1$s %2$s [%3$s]';
  60.         $contextFormat = 'in %1$s on line %2$s';
  61.  
  62.         if (isset($prefs['handler']['display']['lineFormat'])) {
  63.             $lineFormat = $prefs['handler']['display']['lineFormat'];
  64.             $lineFormat = strip_tags($lineFormat);
  65.         }
  66.         if (isset($prefs['handler']['display']['contextFormat'])) {
  67.             $contextFormat = $prefs['handler']['display']['contextFormat'];
  68.             $contextFormat = strip_tags($contextFormat);
  69.         }
  70.  
  71.         $context = $err['context'];
  72.            
  73.         if ($context) {
  74.             $file  = $context['file'];
  75.             $line  = $context['line'];
  76.                
  77.             $contextExec = sprintf($contextFormat, $file, $line);
  78.         } else {
  79.             $contextExec = '';
  80.         }
  81.    
  82.         $msg = sprintf($lineFormat, '', $message, $contextExec);
  83.         return trim($msg);
  84.     }
  85.  
  86.     function getBacktrace()
  87.     {
  88.         if (function_exists('debug_backtrace')) {
  89.             $backtrace = debug_backtrace();
  90.             $backtrace = $backtrace[count($backtrace)-1];
  91.         } else {
  92.             $backtrace = false;
  93.         }
  94.         return $backtrace;
  95.     }
  96.  
  97.     function errorHandler($err)
  98.     {
  99.         global $halt_onException;
  100.        
  101.         if ($halt_onException) {
  102.             if ($err['level'] == 'exception') {
  103.                 return PEAR_ERRORSTACK_DIE;
  104.             }
  105.         }
  106.     }
  107. }
  108.  
  109. function dump($title, $e)
  110. {
  111.     echo "<h1> $title </h1>";
  112.     print_r($e['message']);
  113.     echo '<br/>';
  114. }
  115.  
  116. // set it to on if you want to halt script on any exception
  117. $halt_onException = false;
  118.  
  119.  
  120. // Example A. ---------------------------------------------
  121.  
  122. $stack =& new HTML_Progress2_ErrorStack();
  123.  
  124. $prefs = array('error_handler' => array(&$stack, 'push'));
  125.  
  126. // A1. Exception
  127. $pb1 = new HTML_Progress2($prefs, HTML_PROGRESS2_BAR_VERTICAL, '0', 130);
  128.  
  129.  
  130. $countErrors = $pb1->hasErrors();
  131. for ($i=0; $i<$countErrors; $i++) {
  132.     $e = $pb1->getError();
  133.     dump('html_progress2_errorstack ('.$i.')', $e);
  134. }
  135.  
  136.  
  137. // Example B. ---------------------------------------------
  138.  
  139. $displayConfig = array(
  140.     'lineFormat' => '<b>%1$s</b>: %2$s<br/>%3$s<hr/>',
  141.     'contextFormat' =>   '<b>File:</b> %1$s <br/>'
  142.                        . '<b>Line:</b> %2$s '
  143. );
  144. $prefs = array(
  145.     'error_handler' => array(&$stack, 'push'),
  146.     'handler' => array('display' => $displayConfig)
  147. );
  148.  
  149. $pb2 = new HTML_Progress2($prefs);
  150.  
  151. // B1. Error
  152. $pb2->setMinimum(-1);
  153.  
  154. // B2. Exception
  155. $pb2->setIndeterminate('true');
  156.  
  157. print 'still alive !'
  158.  
  159. ?>