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