Log::DB

Overview

This example will show you how to logs HTML_Progress2 errors into a database
with PEAR::Log and sql driver

Screenshot

HTML_Progress2 error : invalid input, parameter #3 "$min" was expecting "integer", instead got "string"
HTML_Progress2 error : invalid input, parameter #1 "$min" was expecting "positive", instead got "-1"
HTML_Progress2 error : invalid input, parameter #1 "$continuous" was expecting "boolean", instead got "string"


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

still alive !

Demonstration

warning For security reasons live demo is not available

Dependencies

This example requires mandatory resources :

Explains step by step

On example A, all errors will be proceed by the user callback myErrorHandler (line 77), function declared on lines 16-63.

First, you need to build the log_table as declared on lines 87-94 into your database (line 83). You need, of course a user profil (lines 81-82), with write rights.

Second, we will avoid the script to halt on any exception with push_callback option (line 108) that overload the default behaviour. See function myErrorHandler declared on lines 65-69, that returns NULL value.

Third, we will defines PEAR::Log sql driver options (lines 97-104) activated on line 109.

Note: contextFormat option (line 99) is not a Log sql driver option. It's only used by our myErrorHandler user error handler.

Option Value Default
push_callback myErrorHandler HTML_Progress2_Error::_handleError
handler log = array([1])log = array([2])

Custom options of log handler:

log [1] Value
sql['name'] log_table
sql['ident'] HTML_Progress2
sql['conf']['dsn'] mysql://root:****@/test
sql['conf']['contextFormat'] [File="%1\$s" Line="%2\$s"]

Default options of log handler:

log [2] Value
eol \\n
lineFormat %1\$s %2\$s [%3\$s] %4\$s %5\$s
contextFormat in %3\$s (file %1\$s on line %2\$s)
timeFormat %b %d %H:%M:%S
ident IP client address
message_type 3 (=> file destination)
destination html_progress2_error.log
extra_headers  
See also :

Now, that all options are defined, let's have a look on our user error handler (lines 16-63):

This function will send error message on browser and/or into database, depending on the php.ini directives value display_errors, log_errors (lines 18-19).

Printing on browser, is made by a simple printf with the value of message into PEAR_Error object given by value to this function (line 16).

Insert the new error object into database (lines 49-61) is possible because we have created an instance of PEAR::Log sql (DB) driver (line 43).

Lines 28-41 checks if we must used default options (lines 28-29) or custom ones (line 99).

See also :

Source Code

  1. <?php
  2. /**
  3. * Customize error renderer with default PEAR_Error object
  4. * and PEAR::Log (db handler, mysql driver).
  5. *
  6. * @version    $Id: errorlogger.php,v 1.1 2005/06/12 21:03:04 farell Exp $
  7. * @author     Laurent Laville <pear@laurent-laville.org>
  8. * @package    HTML_Progress2
  9. * @subpackage Examples
  10. * @access     public
  11. */
  12. require_once 'HTML/Progress2.php';
  13. require_once 'PEAR.php';
  14. require_once 'Log.php';
  15.  
  16. function myErrorCallback($pb_error)
  17. {
  18.     $display_errors = ini_get('display_errors');
  19.     $log_errors = ini_get('log_errors');
  20.  
  21.     if ($display_errors) {
  22.         printf('<b>HTML_Progress2 error :</b> %s<br/>', $pb_error->getMessage());
  23.     }
  24.  
  25.     if ($log_errors) {
  26.         $userinfo = $pb_error->getUserInfo();
  27.  
  28.         $lineFormat = '%1$s %2$s';
  29.         $contextFormat = '(Function="%3$s" File="%1$s" Line="%2$s")';
  30.  
  31.         $options =& $userinfo['log']['sql'];
  32.         $db_table =& $options['name'];
  33.         $ident =& $options['ident'];
  34.         $conf =& $options['conf'];
  35.  
  36.         if (isset($conf['lineFormat'])) {
  37.             $lineFormat = $conf['lineFormat'];
  38.         }
  39.         if (isset($conf['contextFormat'])) {
  40.             $contextFormat = $conf['contextFormat'];
  41.         }
  42.  
  43.         $logger = &Log::singleton('sql', $db_table, $ident, $conf);
  44.  
  45.         $msg = $pb_error->getMessage();
  46.         $ctx = $pb_error->sprintContextExec($contextFormat);
  47.         $message = sprintf($lineFormat, $msg, $ctx);
  48.  
  49.         switch ($userinfo['level']) {
  50.          case 'exception':
  51.              $logger->alert($message);
  52.              break;
  53.          case 'error':
  54.              $logger->err($message);
  55.              break;
  56.          case 'warning':
  57.              $logger->warning($message);
  58.              break;
  59.          default:
  60.              $logger->notice($message);
  61.         }
  62.     }
  63. }
  64.  
  65. function myErrorHandler()
  66. {
  67.     // always returns error; do not halt script on exception
  68.     return null;
  69. }
  70.  
  71. ini_set('display_errors',1);
  72. ini_set('log_errors',1);
  73.  
  74.  
  75. // Example A. ---------------------------------------------
  76.  
  77. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'myErrorCallback');
  78.  
  79.  
  80. $dbms     = 'mysql';     // your database management system
  81. $db_user  = 'root';      // your database user account
  82. $db_pass  = '****';      // your database user-password account
  83. $db_name  = 'test';      // your database name
  84. $db_table = 'log_table'; // your database log table
  85.  
  86. /**
  87. * CREATE TABLE log_table (
  88. *  id          INT NOT NULL,
  89. *  logtime     TIMESTAMP NOT NULL,
  90. *  ident       CHAR(16) NOT NULL,
  91. *  priority    INT NOT NULL,
  92. *  message     VARCHAR(255),
  93. *  PRIMARY KEY (id)
  94. * );
  95. */
  96.  
  97. $options = array(
  98.     'dsn' => "$dbms://$db_user:$db_pass@/$db_name",
  99.     'contextFormat' => '[File="%1$s" Line="%2$s"]'
  100. );
  101. $sql_handler = array('name' => $db_table,
  102.                      'ident' => 'HTML_Progress2',
  103.                      'conf' => $options
  104.                      );
  105. $logConfig = array('sql' => $sql_handler);
  106.  
  107. $prefs = array(
  108.     'push_callback' => 'myErrorHandler',
  109.     'handler' => array('log' => $logConfig)
  110. );
  111.  
  112. // A1. Exception
  113. $pb1 = new HTML_Progress2($prefs, HTML_PROGRESS2_BAR_VERTICAL, '0', 130);
  114.  
  115. // A2. Error
  116. $pb1->setMinimum(-1);
  117.  
  118. // A3. Exception
  119. $pb1->setIndeterminate('true');
  120.  
  121. if ($pb1->hasErrors()) {
  122.     $msg  = "<br /><hr />";
  123.     $msg .= "Previous errors has been recorded in database '$db_name',";
  124.     $msg .= " table '$db_table'";
  125.     echo "$msg <br /><br />";
  126. }
  127.  
  128. print 'still alive !';
  129.  
  130. ?>