Elgg  Version master
BacktraceProcessor.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Logger;
4 
5 use Elgg\Logger;
6 
11 
12  private $level;
13 
14  private $backtrace_level;
15 
22  public function __construct($level = Logger::WARNING, $backtrace_level = -1) {
23  $this->level = Logger::toMonologLevel($level);
24  $this->backtrace_level = $backtrace_level;
25  }
26 
34  public function __invoke(array $record) {
35  // return if the level is not high enough
36  if ($record['level'] < $this->level) {
37  return $record;
38  }
39 
40  $backtrace_level = $this->backtrace_level;
41 
42  $stack = [];
43  $backtrace = debug_backtrace();
44  // never show this call.
45  $backtrace = array_slice($backtrace, 9); // ignore the monolog stack
46 
47  $i = count($backtrace);
48 
49  foreach ($backtrace as $trace) {
50  if (empty($trace['file'])) {
51  // file/line not set for Closures
52  $stack[] = "[#$i] unknown";
53  } else {
54  $stack[] = "[#$i] {$trace['file']}:{$trace['line']}";
55  }
56 
57  $i--;
58 
59  if ($backtrace_level > 0) {
60  if ($backtrace_level <= 1) {
61  break;
62  }
63 
64  $backtrace_level--;
65  }
66  }
67 
68  $record['extra']['backtrace'] = $stack;
69 
70  return $record;
71  }
72 }
Inject backtrace stack into the record.
__invoke(array $record)
Process recrod.
__construct($level=Logger::WARNING, $backtrace_level=-1)
Constructor.