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 use Monolog\Level;
8 
13 
14  private $level;
15 
16  private $backtrace_level;
17 
24  public function __construct($level = Level::Warning, $backtrace_level = -1) {
25  $this->level = Logger::toMonologLevel($level);
26  $this->backtrace_level = $backtrace_level;
27  }
28 
36  public function __invoke(LogRecord $record): LogRecord {
37  // return if the level is not high enough
38  if ($record->level->isLowerThan($this->level)) {
39  return $record;
40  }
41 
42  $backtrace_level = $this->backtrace_level;
43 
44  $stack = [];
45  $backtrace = debug_backtrace();
46  // never show this call.
47  $backtrace = array_slice($backtrace, 9); // ignore the monolog stack
48 
49  $i = count($backtrace);
50 
51  foreach ($backtrace as $trace) {
52  if (empty($trace['file'])) {
53  // file/line not set for Closures
54  $stack[] = "[#$i] unknown";
55  } else {
56  $stack[] = "[#$i] {$trace['file']}:{$trace['line']}";
57  }
58 
59  $i--;
60 
61  if ($backtrace_level > 0) {
62  if ($backtrace_level <= 1) {
63  break;
64  }
65 
66  $backtrace_level--;
67  }
68  }
69 
70  $record['extra']['backtrace'] = $stack;
71 
72  return $record;
73  }
74 }
Inject backtrace stack into the record.
__invoke(LogRecord $record)
Process record.
__construct($level=Level::Warning, $backtrace_level=-1)
Constructor.