1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
 * Custom error message logging with PEAR_ErrorStack
 *
 * @author     Laurent Laville <pear@laurent-laville.org>
 * @access     public
 * @category   PEAR
 * @package    PEAR_ErrorStack
 * @version    1.4.0
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 */

require_once 'ErrorStack.php';
require_once 'myPHPLogger.php';

define ('MYPACKAGE_ERROR_NOFILE', 10);
define ('MYPACKAGE_ERROR_DBCRIT', 20);
define ('MYPACKAGE_DEBUG_ERRORHANDLING', 30);

function dump($title, $e)
{
    echo "<h1> $title </h1>";
    echo '<pre>';
    var_dump($e);
    echo '</pre>';
    echo '<hr/>';
}

function handleError($err)
{
    if ($err['level'] == 'exception') {
        $message = $err['message'];
        var_dump($err['context']);
        die($message);
    } else {
        printf('<b>%1$s:</b> %2$s ', ucfirst($err['level']), $err['message']); 
        echo '<br/><hr/>';
    }
}

ini_set('display_errors',1);
ini_set('log_errors',1);

$includes = get_included_files();
dump('included_files', $includes);

$global_stack = &PEAR_ErrorStack::singleton('MyPackage');

$globalLog = 'handleError';
PEAR_ErrorStack::setDefaultLogger($globalLog);

$file = 'FTP.php';
$path = str_replace('\\', '/', ini_get('include_path'));
$msg  = 'No file "%file%" found, check the path "%path%"';

$global_stack->push(MYPACKAGE_ERROR_NOFILE, 'warning',
    array('file' => $file, 'path' => $path),
    $msg);

// @see myPHPLogger.php
$global_stack->setLogger($defLog);

$dsn   = 'mysql://user:pass@localhost/db';
$query = 'select * from dual';
$msg   = 'Database Error: Contact Administrator immediately';

$global_stack->push(MYPACKAGE_ERROR_DBCRIT, 'critical',
    array('query' => $query, 'dsn' => $dsn),
    $msg);

// @see myPHPLogger.php
$global_stack->setLogger($customLog);

$handler = sprintf('(%s) %s', gettype($customLog), get_class($customLog));
$msg     = 'Error Handler = %errorHandling%';

$global_stack->push(MYPACKAGE_DEBUG_ERRORHANDLING, 'debug',
    array('errorHandling' => $handler),
    $msg);

$errors = $global_stack->getErrors();
dump('MyPackage error_stack', $errors);
?>