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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
 * Custom error logger using only PHP functions 
 * with a PEAR_ErrorStack contents
 *
 * @author     Laurent Laville <pear@laurent-laville.org>
 * @access     public
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 */

class myPHPLogger 
{
    var $_display;
    var $_log;
    
    function myPHPLogger($displayConf = array(), $logConf = array())
    {
        $displayDefault = array(
            'lineFormat' => '<b>%1$s</b>: %2$s %3$s <br/>',
            'contextFormat' => ' in <b>%3$s</b> (file <b>%1$s</b> at line <b>%2$s</b>)'
        );
        $this->_display = array_merge($displayDefault, $displayConf);

        $logDefault = array(
            'eol' => "\n",
            'lineFormat' => '%1$s %2$s [%3$s] %4$s %5$s',
            'timeFormat' => '%b %d %H:%M:%S',
            'ident' => $_SERVER['REMOTE_ADDR'],
            'message_type' => 3,
            'destination' => 'myerrors.log',
            'extra_headers' => ''
        );
        $this->_log = array_merge($logDefault, $logConf);
    }
    
    function log($err)
    {
        if (isset($err['context'])) {
            $context = $err['context'];
        } else {
            $context = false;
        }
   
        if ($context) {
            $file  = $context['file'];
            $line  = $context['line'];
            
            if (isset($context['class'])) {
                $func  = $context['class'];
                $func .= $context['type'];
                $func .= $context['function'];
            } elseif (isset($context['function'])) {
                $func  = $context['function'];
            } else {
                $func  = '';
            }
        }

        $display = $display_errors = ini_get('display_errors');
        $log = $log_errors = ini_get('log_errors');

        if ($display) {
            $lineFormat = $this->_display['lineFormat'];
            if (substr($lineFormat, -1) != "\n") {
                $lineFormat .= "\n";
            }
            $contextFormat = $this->_display['contextFormat'];
            if (!$context) {
                $contextExec = '';
            } else {
                $contextExec = sprintf($contextFormat, $file, $line, $func);
            }
           
            printf($lineFormat, ucfirst($err['level']), $err['message'], $contextExec);
            echo '<br/><hr/>';
        }

        if ($log) {
            $message_type = $this->_log['message_type'];
            $destination = '';
            $extra_headers = '';
            $send = true;
                
            switch ($message_type) {
                case 0:  // LOG_TYPE_SYSTEM:
                    break;
                case 1:  // LOG_TYPE_MAIL:
                    $destination = $this->_log['destination'];
                    $extra_headers = $this->_log['extra_headers'];
                    break;
                case 3:  // LOG_TYPE_FILE:
                    $destination = $this->_log['destination'];
                    break;
                default:
                    $send = false;
            }

            if ($send) {
                $contextExec = sprintf($this->_display['contextFormat'], $file, $line, $func);
                $message = sprintf($this->_log['lineFormat'] . $this->_log['eol'], 
                               strftime($this->_log['timeFormat'], $err['time']),
                               $this->_log['ident'],
                               $err['level'],
                               $err['message'],
                               $contextExec);

                error_log(strip_tags($message), $message_type, $destination, $extra_headers);
            }
        }
    }
}

// uses default options
$defLog = new myPHPLogger();

// another example of error message look and feel
$displayConfig = array(
    'lineFormat' => '<b>%1$s</b>: %2$s <br/>%3$s<br/>',
    'contextFormat' =>   '&nbsp;&nbsp;<i>File:</i> %1$s <br />'
                       . '&nbsp;&nbsp;<i>Line:</i> %2$s <br />'
                       . '&nbsp;&nbsp;<i>Function:</i> %3$s '
);
$customLog = new myPHPLogger($displayConfig);
?>