Elgg  Version 6.3
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;
7 use Monolog\LogRecord;
8 
13 
14  protected Level $level;
15 
22  public function __construct($level = Level::Warning, protected int $backtrace_level = -1) {
23  $this->level = Logger::toMonologLevel($level);
24  }
25 
33  public function __invoke(LogRecord $record): LogRecord {
34  // return if the level is not high enough
35  if ($record->level->isLowerThan($this->level)) {
36  return $record;
37  }
38 
39  if (isset($record->context['throwable'])) {
40  // rely on default output
41  return $record;
42  }
43 
44  $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
45  foreach ($backtrace as $index => $trace) {
46  if (isset($trace['file'])) {
47  // strip the monolog stack
48  if (str_contains($trace['file'], '\Monolog\\')) {
49  unset($backtrace[$index]);
50  continue;
51  }
52 
53  if (str_contains($trace['file'], '\Elgg\Logger.php')) {
54  unset($backtrace[$index]);
55  continue;
56  }
57 
58  if (str_contains($trace['file'], '\lib\elgglib.php')) {
59  unset($backtrace[$index]);
60  continue;
61  }
62 
63  break;
64  }
65  }
66 
67  $i = count($backtrace);
68  $backtrace_level = $this->backtrace_level;
69 
70  $stack = [];
71  foreach ($backtrace as $trace) {
72  if (empty($trace['file'])) {
73  // file/line not set for Closures
74  $stack[] = "[#$i] unknown";
75  } else {
76  $stack[] = "[#$i] {$trace['file']}:{$trace['line']}";
77  }
78 
79  $i--;
80 
81  if ($backtrace_level > 0) {
82  if ($backtrace_level <= 1) {
83  break;
84  }
85 
86  $backtrace_level--;
87  }
88  }
89 
90  $record['extra']['backtrace'] = $stack;
91 
92  return $record;
93  }
94 }
Inject backtrace stack into the record.
__construct($level=Level::Warning, protected int $backtrace_level=-1)
Constructor.
__invoke(LogRecord $record)
Process record.
$index
Definition: gallery.php:40
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10