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
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
<?php
/**
* Customize error renderer with default PEAR_Error object
* and PEAR::Log (db handler, mysql driver).
*
* @version $Id: errorlogger.php,v 1.1 2005/06/12 21:03:04 farell Exp $
* @author Laurent Laville <pear@laurent-laville.org>
* @package HTML_Progress2
* @subpackage Examples
* @access public
*/
require_once 'HTML/Progress2.php';
require_once 'PEAR.php';
require_once 'Log.php';
function myErrorCallback($pb_error)
{
$display_errors = ini_get('display_errors');
$log_errors = ini_get('log_errors');
if ($display_errors) {
printf('<b>HTML_Progress2 error :</b> %s<br/>', $pb_error->getMessage());
}
if ($log_errors) {
$userinfo = $pb_error->getUserInfo();
$lineFormat = '%1$s %2$s';
$contextFormat = '(Function="%3$s" File="%1$s" Line="%2$s")';
$options =& $userinfo['log']['sql'];
$db_table =& $options['name'];
$ident =& $options['ident'];
$conf =& $options['conf'];
if (isset($conf['lineFormat'])) {
$lineFormat = $conf['lineFormat'];
}
if (isset($conf['contextFormat'])) {
$contextFormat = $conf['contextFormat'];
}
$logger = &Log::singleton('sql', $db_table, $ident, $conf);
$msg = $pb_error->getMessage();
$ctx = $pb_error->sprintContextExec($contextFormat);
$message = sprintf($lineFormat, $msg, $ctx);
switch ($userinfo['level']) {
case 'exception':
$logger->alert($message);
break;
case 'error':
$logger->err($message);
break;
case 'warning':
$logger->warning($message);
break;
default:
$logger->notice($message);
}
}
}
function myErrorHandler()
{
// always returns error; do not halt script on exception
return null;
}
ini_set('display_errors',1);
ini_set('log_errors',1);
// Example A. ---------------------------------------------
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'myErrorCallback');
$dbms = 'mysql'; // your database management system
$db_user = 'root'; // your database user account
$db_pass = '****'; // your database user-password account
$db_name = 'test'; // your database name
$db_table = 'log_table'; // your database log table
/**
* CREATE TABLE log_table (
* id INT NOT NULL,
* logtime TIMESTAMP NOT NULL,
* ident CHAR(16) NOT NULL,
* priority INT NOT NULL,
* message VARCHAR(255),
* PRIMARY KEY (id)
* );
*/
$options = array(
'dsn' => "$dbms://$db_user:$db_pass@/$db_name",
'contextFormat' => '[File="%1$s" Line="%2$s"]'
);
$sql_handler = array('name' => $db_table,
'ident' => 'HTML_Progress2',
'conf' => $options
);
$logConfig = array('sql' => $sql_handler);
$prefs = array(
'push_callback' => 'myErrorHandler',
'handler' => array('log' => $logConfig)
);
// A1. Exception
$pb1 = new HTML_Progress2($prefs, HTML_PROGRESS2_BAR_VERTICAL, '0', 130);
// A2. Error
$pb1->setMinimum(-1);
// A3. Exception
$pb1->setIndeterminate('true');
if ($pb1->hasErrors()) {
$msg = "<br /><hr />";
$msg .= "Previous errors has been recorded in database '$db_name',";
$msg .= " table '$db_table'";
echo "$msg <br /><br />";
}
print 'still alive !';
?>