IntrospectionProcessor.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Processor;
  11. use Monolog\Logger;
  12. /**
  13. * Injects line/file:class/function where the log message came from
  14. *
  15. * Warning: This only works if the handler processes the logs directly.
  16. * If you put the processor on a handler that is behind a FingersCrossedHandler
  17. * for example, the processor will only be called once the trigger level is reached,
  18. * and all the log records will have the same file/line/.. data from the call that
  19. * triggered the FingersCrossedHandler.
  20. *
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class IntrospectionProcessor implements ProcessorInterface
  24. {
  25. private $level;
  26. private $skipClassesPartials;
  27. private $skipStackFramesCount;
  28. private $skipFunctions = [
  29. 'call_user_func',
  30. 'call_user_func_array',
  31. ];
  32. /**
  33. * @param string|int $level The minimum logging level at which this Processor will be triggered
  34. */
  35. public function __construct($level = Logger::DEBUG, array $skipClassesPartials = [], int $skipStackFramesCount = 0)
  36. {
  37. $this->level = Logger::toMonologLevel($level);
  38. $this->skipClassesPartials = array_merge(['Monolog\\'], $skipClassesPartials);
  39. $this->skipStackFramesCount = $skipStackFramesCount;
  40. }
  41. public function __invoke(array $record): array
  42. {
  43. // return if the level is not high enough
  44. if ($record['level'] < $this->level) {
  45. return $record;
  46. }
  47. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  48. // skip first since it's always the current method
  49. array_shift($trace);
  50. // the call_user_func call is also skipped
  51. array_shift($trace);
  52. $i = 0;
  53. while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
  54. if (isset($trace[$i]['class'])) {
  55. foreach ($this->skipClassesPartials as $part) {
  56. if (strpos($trace[$i]['class'], $part) !== false) {
  57. $i++;
  58. continue 2;
  59. }
  60. }
  61. } elseif (in_array($trace[$i]['function'], $this->skipFunctions)) {
  62. $i++;
  63. continue;
  64. }
  65. break;
  66. }
  67. $i += $this->skipStackFramesCount;
  68. // we should have the call source now
  69. $record['extra'] = array_merge(
  70. $record['extra'],
  71. [
  72. 'file' => isset($trace[$i - 1]['file']) ? $trace[$i - 1]['file'] : null,
  73. 'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
  74. 'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null,
  75. 'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
  76. ]
  77. );
  78. return $record;
  79. }
  80. private function isTraceClassOrSkippedFunction(array $trace, int $index)
  81. {
  82. if (!isset($trace[$index])) {
  83. return false;
  84. }
  85. return isset($trace[$index]['class']) || in_array($trace[$index]['function'], $this->skipFunctions);
  86. }
  87. }