summaryrefslogtreecommitdiffstats
path: root/error_log-to-currdir.php
blob: 32d1de412f2426365d86669cf137742257cd145d (plain)
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
<?php

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('log_errors_max_len', 0); /* unlimited */
ini_set('error_log', './php_errors.log');

register_shutdown_function(function() {
    // error_log func not available here due to fatal php shutdown
    //error_log(print_r(debug_backtrace(), TRUE));

});

function a($arg) {
    b('beta');
}

function b($arg) {
    c('charlie');
}

function c($arg) {
    d('delta');
}

function d($arg) {
    /* calling error_log in register_shutdown_function is too late, however var_dump still works there, in case of error
     * in real code insert the line below above the line causing trouble
     */
    error_log(print_r(debug_backtrace(), TRUE));

    /* nicer output */
    $e = new Exception;
    error_log($e->getTraceAsString());

    /* print to screen in simple format */
    print mydebug_bt();

    trigger_error("user triggered error", E_USER_ERROR);
}

error_log('calling a()', 0, './php_errors.log');
a('alpha');

/* simplify and beatify the output of a dump of debug_backtrace() */
function mydebug_bt() {
    $stack = '';
    $i = 1;
    $trace = debug_backtrace();
    unset($trace[0]); // remove call to this function from stack trace
    foreach($trace as $node) {
        $stack .= "#$i ".$node['file'] ."(" .$node['line']."): ";
        if(isset($node['class'])) {
            $stack .= $node['class'] . "->";
        }
        $stack .= $node['function'] . "()" . PHP_EOL;
        $i++;
    }
    return $stack;
}