Examples TOCexamples

Error_Log handler - part 3.

$Date: 2004/08/07 14:28:59 $

 Table of contents

Introduction

This example requires :


This example will show you how to handle html_progress errors with a dbms.

An error was volontary made on call of setAnimSpeed method (line 80). As it's an API error, default is to return error after it was raised, so lines below line 81 will be executed. Error is also logged into table 'log_table' of database 'test'.

We have defined (lines 4 to 44) a new user error handler for html_progress (line 77). In this handler, we will send error message on browser by using PEAR_Error (print mode) object (lines 11 to 16). We will also logged the same error content on a database (lines 18 to 41). DB account is defined on lines 49 to 53 (and only there).

display_errors

Print out errors (as a part of the output). For production web sites, you're strongly encouraged to turn this feature off, and use error logging instead. Keeping display_errors enabled on a production web site may reveal security information to end users, such as file paths on your Web server, your database schema or other information.

1 <?php ini_set('display_errors', false); ?>

log_errors

Log errors into a log file (server-specific log, stderr, syslog ...) As stated above, you're strongly advised to use error logging in place of error displaying on production web sites.

1 <?php ini_set('log_errors', true); ?>

 Render options

Default options below are used :

Only option changed is :

Legend for lineFormat: Legend for contextFormat:

[Top]

 Output

HTML Progress Error : invalid input, parameter #1 "$delay" was expecting "less or equal 1000", instead got "10000"


Previous error has been recorded in database 'test', table 'log_table'

[Top]

 PHP source syntax highlight

  1. <?php
  2. require_once 'HTML/Progress.php';
  3.  
  4. function _errorHandler($err)
  5. {
  6.     global $sql_handler;
  7.    
  8.     $display_errors = ini_get('display_errors');
  9.     $log_errors = ini_get('log_errors');
  10.    
  11.     if ($display_errors) {
  12.         include_once 'PEAR.php';
  13.         $e = PEAR::raiseError($err['message'], $err['code'],
  14.                               PEAR_ERROR_PRINT, '<b>HTML Progress Error :</b> %s',
  15.                               $err['context']);
  16.     }
  17.  
  18.     if ($log_errors) {
  19.         include_once 'DB.php';
  20.      
  21.         $db = &DB::connect($sql_handler['conf']['dsn']);
  22.         if (DB::isError($db)) {
  23.             return $db;
  24.         }
  25.         $id = $db->nextId('log_id');
  26.         $q = sprintf('insert into %s (id, logtime, ident, priority, message)' .
  27.                      'values(%d, %d, %s, %s, %s)',
  28.                      $sql_handler['name'],
  29.                      $id,
  30.                      $err['time'],
  31.                      $db->quote($sql_handler['ident']),
  32.                      $db->quote($err['level']),
  33.                      $db->quote($err['message']));
  34.  
  35.         $result = $db->query($q);
  36.         if (DB::isError($result)) {
  37.             return $db;
  38.         }
  39.        
  40.         $db->disconnect();
  41.     }
  42.  
  43.     return $e;
  44. }
  45.  
  46. ini_set('display_errors', true);  // in developement we may accept browser outputs
  47. ini_set('log_errors', true);      // be sure to logs errors
  48.  
  49. $dbms     = 'mysql';     // your database management system
  50. $db_user  = 'root';      // your database user account
  51. $db_pass  = '****';      // your database user-password account
  52. $db_name  = 'test';      // your database name
  53. $db_table = 'log_table'; // your database log table
  54.  
  55. /**
  56.  * CREATE TABLE log_table (
  57.  *  id          INT NOT NULL,
  58.  *  logtime     INT NOT NULL,
  59.  *  ident       CHAR(16) NOT NULL,
  60.  *  priority    CHAR(12) NOT NULL,
  61.  *  message     VARCHAR(200),
  62.  *  PRIMARY KEY (id)
  63.  * );
  64.  */
  65.  
  66. $options = array(
  67.     'dsn' => "$dbms://$db_user:$db_pass@/$db_name",
  68.     'lineFormat' => '%4$s (context: %5$s)',
  69.     'contextFormat' => 'Function="%3$s" File="%1$s" Line="%2$s"'
  70. );
  71. $sql_handler = array('name' => $db_table,
  72.                      'ident' => 'HTML_Progress',
  73.                      'conf' => $options
  74.                      );
  75.  
  76. $logger['handler']['sql'] =& $sql_handler;
  77. $logger['error_handler']  = '_errorHandler';
  78.  
  79. $bar = new HTML_Progress($logger);
  80. $e = $bar->setAnimSpeed(10000);   // < - - - will generate an API error
  81.  
  82. if ($bar->hasErrors()) {
  83.     $msg  = "<br/><hr/>";
  84.     $msg .= "Previous error has been recorded in database '$db_name', table '$db_table'";
  85.     die($msg);
  86. }
  87. ?>

[Top]